SFML community forums
Help => Graphics => Topic started by: Auron on August 05, 2009, 07:10:40 pm
-
Hey Guys,
I went through the examples and tried displaying an image on the screen, but it won't shop up. My code is right and it compiles without errors, but the window is blank. Here's my code.
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <time.h>
#include <string.h>
#include <wchar.h>
#include <SFML/System.hpp>
#include <SFML/Window.hpp>
#include <SFML/Graphics.hpp>
#include <SFML/Audio.hpp>
#include <SFML/Network.hpp>
#include "player.h"
#include "vehicle.h"
int main()
{
sf::RenderWindow App(sf::VideoMode(800,600,32), "Car Junker - DarkScar Games");
const sf::Input& Input = App.GetInput();
bool Logo_Screen = true;
bool Title_Screen = false;
sf::Image TitleScr;
sf::Image LogoScr;
if(!TitleScr.LoadFromFile("Title.png"))
{
return EXIT_FAILURE;
}
sf::Sprite TitleSprt(TitleScr);
if(!LogoScr.LoadFromFile("Logo.png"))
{
return EXIT_FAILURE;
}
sf::Sprite LogoSprt(LogoScr);
LogoSprt.SetImage(LogoScr);
bool LeftKeyDown = Input.IsKeyDown(sf::Key::Left);
bool RightKeyDown = Input.IsKeyDown(sf::Key::Right);
bool UpKeyDown = Input.IsKeyDown(sf::Key::Up);
bool DownKeyDown = Input.IsKeyDown(sf::Key::Down);
bool SpaceDown = Input.IsKeyDown(sf::Key::Space);
bool LeftMouseDown = Input.IsMouseButtonDown(sf::Mouse::Left);
bool RightMouseDown = Input.IsMouseButtonDown(sf::Mouse::Right);
unsigned int MouseX = Input.GetMouseX();
unsigned int MouseY = Input.GetMouseY();
Player* gPlayer = new Player();
Vehicle* gVehicle = new Vehicle();
sf::Clock Clock;
while(App.IsOpened())
{
sf::Event Event;
App.SetFramerateLimit(60);
while(App.GetEvent(Event))
{
if(Event.Type == sf::Event::Closed)
{
App.Close();
}
if(Event.Key.Code == sf::Key::Escape)
{
App.Close();
}
if(Event.Key.Code == sf::Key::Left)
{
LeftKeyDown = true;
gPlayer->X = gPlayer->X - 5;
}
else if(Event.Key.Code == sf::Key::Right)
{
RightKeyDown = true;
gPlayer->X = gPlayer->X + 5;
}
else if(Event.Key.Code == sf::Key::Up)
{
UpKeyDown = true;
gPlayer->Y = gPlayer->Y - 5;
}
else if(Event.Key.Code == sf::Key::Down)
{
DownKeyDown = true;
gPlayer->Y = gPlayer->Y + 5;
}
}
App.Draw(LogoSprt);
App.Clear(sf::Color(0,0,0,0));
App.Display();
}
return EXIT_SUCCESS;
}
-
http://www.sfml-dev.org/wiki/en/faq#my_sprite_isn_t_displayed_but_a_white_square ( ? )
About your code :
-> you should use the C++ version of time.h, math.h, ... : ctime, cmath, c... . Exept for string.h. You should use std::string instead of char* ( from string header ) .
-> here you need only SFML/Graphics.hpp and cstdlib. ( You don't the other ones. )
-> there are two new, but no delete.
-> you should create more function.
and more.
-
Ah, ok, I'll try re-working my code.
EDIT: I used SetImage(), but nothing is still displayed on the screen.
-
You're testing Event members without first checking which type of events it is. So you may, for example, retrieve the event's key code when it is a MouseMove event (which results in undefined behaviour).
-
Ah, ok, I need read the docs more carefully then.
-
You're clearing the screen after you draw things. That's why you don't see anything on the screen.
Also what Laurent said about the event checking is pretty obvious, too.
-
Yes, I am working on it. I have a got an image to show up on the screen. I just need to work with the SFML library more to get the hang of things.