Hello!
I've only just started to learn SDL. I've got some previous knowledge on OpenGL from when i was in school (created a simple game in class).
Anyways I've followed Lazy Foo' Productions tuturial on SDL to become accustomed with the new functions ect. One of the mayor things i've notices in different is how you draw on the screen. If i understand this right i have to write a code similar to this to get a image to show in SDL:
[source lang="cpp"]SDL_Surface *screen = NULL;
SLD_Surface *image = IMG_load("hello.png);
SDL_BlitSurface(image, NULL, screen, NULL);
SDL_Flip(screen)// to update the scene[/source]
Now this isn't to hard if you've only got one file, i did however run into some problems when i added a Player class.
When i worked in C++/OpenGL all i had to do was to add stuff into the "renderScene" function which was easily done by doing this for example: "player.draw();" withing in renderScene function.
I am now stuck since i don't know how to transfer the image of my character (from the player class) into main to then get blitted onto the screen.
Could anyone give me some insight of how to tackle this problem? Seems much simpler in OpenGL to draw stuff to the scene.
Thanks in advance!
Learning SDL, need help to draw an object on screen between classes
Ok, i've actually managed to do this now. Hurray!
Seems a but complicated thought so i'm not sure if i am doing it the right way, if someone could have a look on my code i'd really appreciate the help.
There isn't much code at all. The code i want looked over is in the function "renderScene" where we grab the image from the player class. In the player class there is a little code in the function "drawCharacter".
// Here is the download link of the project: https://www.dropbox.com/s/l0dmdbk6z2s1ymw/RPG.rar
Seems a but complicated thought so i'm not sure if i am doing it the right way, if someone could have a look on my code i'd really appreciate the help.
There isn't much code at all. The code i want looked over is in the function "renderScene" where we grab the image from the player class. In the player class there is a little code in the function "drawCharacter".
// Here is the download link of the project: https://www.dropbox.com/s/l0dmdbk6z2s1ymw/RPG.rar
There is a problem with the solution you found. You are loading image each frame. It's very slow!
A better way might be:
Even better would be to store all images in an image loader class that would free all of them automatically when it is destroyed. Then destroying the image in Player destructor wouldn't be necessary.
A better way might be:
class Player {
private:
SDL_Surface* image;
public:
Player() {
SLD_Surface *image = IMG_load("hello.png"); // Load image when creating the player
}
~Player() {
SDL_FreeSurface(image); // Free the image when destroying the player
}
void draw(SDL_Surface* screen) {
SDL_BlitSurface(image, NULL, screen, NULL); // Draw the player image on screen
}
}
// in main loop
// ...
player.draw(screen);
SDL_Flip(screen)// to update the scene
// ...
Even better would be to store all images in an image loader class that would free all of them automatically when it is destroyed. Then destroying the image in Player destructor wouldn't be necessary.
There is a problem with the solution you found. You are loading image each frame. It's very slow!
A better way might be:
class Player {
private:
SDL_Surface* image;
public:
Player() {
SLD_Surface *image = IMG_load("hello.png"); // Load image when creating the player
}
~Player() {
SDL_FreeSurface(image); // Free the image when destroying the player
}
void draw(SDL_Surface* screen) {
SDL_BlitSurface(image, NULL, screen, NULL); // Draw the player image on screen
}
}
// in main loop
// ...
player.draw(screen);
SDL_Flip(screen)// to update the scene
// ...
Even better would be to store all images in an image loader class that would free all of them automatically when it is destroyed. Then destroying the image in Player destructor wouldn't be necessary.
Thanks! Guess my code was kind of sloppy I'll use the code you suggested, much cleaner.
About the image loader class. Basically i'm gonna start with very "trival" features and work me way up and I've already planned what might be needed and a image loader class would be very useful. Planning on using tiles as well and building a system that allows you to customize your character a bit. But at the moment i feel kind of crippled because there was quite a long time since i coded C++ and SDL is all new to me. But i'm learning!
A small change:
class Player {
private:
SDL_Surface* image;
// better disable the copy constructor and the assignment operators because you didn't define them
Player(const Player &);
Player &operator = (const Player &);
public:
Player() {
SLD_Surface *image = IMG_load("hello.png"); // Load image when creating the player
}
~Player() {
SDL_FreeSurface(image); // Free the image when destroying the player
}
void draw(SDL_Surface* screen) {
SDL_BlitSurface(image, NULL, screen, NULL); // Draw the player image on screen
}
}
// in main loop
// ...
player.draw(screen);
SDL_Flip(screen)// to update the scene
// ...
Thanks so very much for the help, made me understand this library some more ;)
I have another question if you are willing to answer it though. I am like i said aiming to create a game so i was thinking that i should probably have a main class named "game" where i create all objects ect... so that i may reach them easily from other classes later on.
I am wondering how i address this in the main file. At the moment I've included the class and this is where i split and dont really know how to continue.
[source lang="cpp"]//Do i simply do this
Game game;
//and then call functions from the game like this:
game.keyboardevents();
//or is it wiser to create a object of main like this:
Game *game;
...
game = new Game();
//and then call functions like this:
game->keyboardevents();[/source]
What is the smartest way to do this?
And by adding the Game class that basically means that i have to create a image of all i want to see in the game class and then send it to main to be displayed? right?
I have another question if you are willing to answer it though. I am like i said aiming to create a game so i was thinking that i should probably have a main class named "game" where i create all objects ect... so that i may reach them easily from other classes later on.
I am wondering how i address this in the main file. At the moment I've included the class and this is where i split and dont really know how to continue.
[source lang="cpp"]//Do i simply do this
Game game;
//and then call functions from the game like this:
game.keyboardevents();
//or is it wiser to create a object of main like this:
Game *game;
...
game = new Game();
//and then call functions like this:
game->keyboardevents();[/source]
What is the smartest way to do this?
And by adding the Game class that basically means that i have to create a image of all i want to see in the game class and then send it to main to be displayed? right?
In this case the 'game' object works fine being allocated on the stack in main(). No need to add indirection unless there's a compelling reason otherwise.
And by adding the Game class that basically means that i have to create a image of all i want to see in the game class and then send it to main to be displayed? right? [/quote]
Wrong. main doesn't need to be used to display the image on the screen. You can call SDL_Flip (and any other SDL functions) within any other class you write if you so desire.
In this case the 'game' object works fine being allocated on the stack in main(). No need to add indirection unless there's a compelling reason otherwise.
And by adding the Game class that basically means that i have to create a image of all i want to see in the game class and then send it to main to be displayed? right?
Wrong. main doesn't need to be used to display the image on the screen. You can call SDL_Flip (and any other SDL functions) within any other class you write if you so desire.
[/quote]
Oh, ok. Then what would you recommend? To screen out all the graphics in the Game class or send it to main to do the job.
If you recommend the first choice then what is it exactly that main does more than create the window for the game to be shown in and run the main game loop?
Is it even necessarily to have a Game class?
I am of course going to split up every part of the game as much as i can for the simplicity it brings.
If you want to see an example of an Imagge loading class built in SDL, I've put an explanation of how I built my own ImageCache class up on my blog at http://sednihp.wordpress.com/2012/06/02/111/
It deals with loading and storing images so that theyre only loaded once per game, and also works with text using sdl_ttf.
It deals with loading and storing images so that theyre only loaded once per game, and also works with text using sdl_ttf.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement