• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
Tallkotten

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

11 posts in this topic

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!
0

Share this post


Link to post
Share on other sites
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: [url="https://www.dropbox.com/s/l0dmdbk6z2s1ymw/RPG.rar"]https://www.dropbox.com/s/l0dmdbk6z2s1ymw/RPG.rar[/url] Edited by Tallkotten
0

Share this post


Link to post
Share on other sites
There is a problem with the solution you found. You are loading image each frame. It's very slow!

A better way might be:
[code]
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
// ...
[/code]

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
1

Share this post


Link to post
Share on other sites
[quote name='mrjones' timestamp='1340897739' post='4953685']
There is a problem with the solution you found. You are loading image each frame. It's very slow!

A better way might be:
[code]
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
// ...
[/code]

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.
[/quote]

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

Share this post


Link to post
Share on other sites
A small change:

[code]
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
// ...
[/code]
2

Share this post


Link to post
Share on other sites
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? Edited by Tallkotten
0

Share this post


Link to post
Share on other sites
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.

[quote]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.
0

Share this post


Link to post
Share on other sites
[quote name='nobodynews' timestamp='1341003525' post='4954090']
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.

[quote]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.
[/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. Edited by Tallkotten
0

Share this post


Link to post
Share on other sites
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.
1

Share this post


Link to post
Share on other sites
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 :)
0

Share this post


Link to post
Share on other sites
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
1

Share this post


Link to post
Share on other sites
[quote name='sednihp' timestamp='1341259173' post='4955018']
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. [img]http://public.gamedev.net//public/style_emoticons/default/smile.png[/img]
[/quote]

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!
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0