Jump to content

  • Log In with Google      Sign In   
  • Create Account


Learning SDL, need help to draw an object on screen between classes


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
11 replies to this topic

#1 Tallkotten   Members   -  Reputation: 288

Like
0Likes
Like

Posted 28 June 2012 - 08:17 AM

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!

Sponsor:

#2 Tallkotten   Members   -  Reputation: 288

Like
0Likes
Like

Posted 28 June 2012 - 09:24 AM

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

Edited by Tallkotten, 28 June 2012 - 09:25 AM.


#3 mrjones   Members   -  Reputation: 612

Like
1Likes
Like

Posted 28 June 2012 - 09:35 AM

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.

Edited by mrjones, 28 June 2012 - 09:37 AM.


#4 Tallkotten   Members   -  Reputation: 288

Like
0Likes
Like

Posted 28 June 2012 - 10:06 AM

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 :P 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! :)

#5 sheep19   Members   -  Reputation: 392

Like
2Likes
Like

Posted 29 June 2012 - 02:39 AM

A small change:

class Player {
  private:
	SDL_Surface* image;

	 // better disable the copy constructor and the assignment operators because you didn't define them <img src='http://public.gamedev.net//public/style_emoticons/<#EMO_DIR#>/smile.png' class='bbc_emoticon' alt=':)' />
	 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
// ...


#6 Tallkotten   Members   -  Reputation: 288

Like
0Likes
Like

Posted 29 June 2012 - 02:14 PM

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 thisGame 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?

Edited by Tallkotten, 29 June 2012 - 02:15 PM.


#7 nobodynews   Crossbones+   -  Reputation: 1842

Like
0Likes
Like

Posted 29 June 2012 - 02:58 PM

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.

C++: A Dialog | C++0x Features: Part1 (lambdas, auto, static_assert) , Part 2 (rvalue references) , Part 3 (decltype) | Write Games | Fix Your Timestep!


#8 Tallkotten   Members   -  Reputation: 288

Like
0Likes
Like

Posted 29 June 2012 - 03:12 PM

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.


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.

Edited by Tallkotten, 29 June 2012 - 03:20 PM.


#9 sednihp   Members   -  Reputation: 241

Like
1Likes
Like

Posted 01 July 2012 - 03:28 PM

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.

#10 Tallkotten   Members   -  Reputation: 288

Like
0Likes
Like

Posted 02 July 2012 - 02:58 AM

Thanks, I've made myself a loader class but i'm guessing it's pretty primitive compared to yours! I'll definitively take a look :)

#11 sednihp   Members   -  Reputation: 241

Like
1Likes
Like

Posted 02 July 2012 - 01:59 PM

The problem with that post is that since then, as with all code, the ImageCache has undergone even more refactoring in other projects that I'm working on, so it's out of date now! But it gets the important ideas across and will hopefully help you and others to build one of your own. :)

Edited by sednihp, 02 July 2012 - 02:00 PM.


#12 Tallkotten   Members   -  Reputation: 288

Like
0Likes
Like

Posted 02 July 2012 - 02:04 PM

The problem with that post is that since then, as with all code, the ImageCache has undergone even more refactoring in other projects that I'm working on, so it's out of date now! But it gets the important ideas across and will hopefully help you and others to build one of your own. Posted Image


Oh, well like i said i've already got a simple one that works for the little code i've got at the moment. When i do decide to build upon my loader class i'll be looking to build something of my own so you are totally right!




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS