Dilemma with SDL screen surface

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
User avatar
hurstshifter
ES Beta Backer
ES Beta Backer
Posts: 713
Joined: Mon Jun 08, 2009 8:33 pm
Favorite Gaming Platforms: SNES
Programming Language of Choice: C/++
Location: Boston, MA
Contact:

Dilemma with SDL screen surface

Post by hurstshifter »

Hey Everyone,

So lately I've been running into a design problem that I can't seem to figure out a solution for. In all the games I create with SDL/C++ I'm never able determine the best way to implement my 'screen' into the code so that it makes for a well-organized and easily accessible setup. In my very first game I had a set of header and source files called 'globals' and ended up instantiating an SDL_Surface* screen within that and including it in every source file that needed it. UGLY!!! My solution the 2nd was to instantiate the screen within the main() and simply pass a reference to it into any function that needed to draw to the buffer. At first this was ok because I had like one or two functions calling all the drawing, but over time I kept needing more and more access to the screen until eventually I had a shitload of screen arguments getting tossed around like a salad. Once again, UGGGGLLYY!!!!

So here I am...stuck. I was hoping someone might be able to point me in the general direction of what might be a better (not necessarily the best) design pattern to follow here. I remember a couple months ago there was a thread about creating the screen as a singleton but that seemed to get shot down as another bad design. Thanks in advance for any help.

-hurst
"Time is an illusion. Lunchtime, doubly so."
http://www.thenerdnight.com
User avatar
GroundUpEngine
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 835
Joined: Sun Nov 08, 2009 2:01 pm
Current Project: mixture
Favorite Gaming Platforms: PC
Programming Language of Choice: C++
Location: UK

Re: Dilemma with SDL screen surface

Post by GroundUpEngine »

This is how I do my thang ;)

Code: Select all

class Window {
	SDL_Surface* MainWindow; // for SDL
}
class Video {
	SDL_Surface* screen;

	void registerRenderer(WindowManager* wind) {
	    // "register" renderer
	    screen = wind;
	    ...
	}
}

Code: Select all

class Game {
	Video* subsystem1;
	Window* renderer;
	
	void Initialize(...) {
		renderer->Initialize(...); // init window
		subsystem1->registerRenderer(renderer); // register window
		subsystem1->Initialize(); // init graphics
		...
	}
	void DrawPlaya(Sprite& sprite) { // remember this?
		subsystem1->DrawSprite(sprite);
	}
}
User avatar
lotios611
Chaos Rift Regular
Chaos Rift Regular
Posts: 160
Joined: Sun Jun 14, 2009 12:05 pm
Current Project: Game engine for the PC, PSP, and maybe more.
Favorite Gaming Platforms: Gameboy Micro
Programming Language of Choice: C++

Re: Dilemma with SDL screen surface

Post by lotios611 »

You don't even need to have an SDL_Surface for the screen. All you have to do is when you initialize the screen, do "SDL_SetVideoMode(args);" then when you want access to the screen, you do "SDL_GetVideoSurface();"
"Why geeks like computers: unzip, strip, touch, finger, grep, mount, fsck, more, yes, fsck, fsck, fsck, umount, sleep." - Unknown
User avatar
dandymcgee
ES Beta Backer
ES Beta Backer
Posts: 4709
Joined: Tue Apr 29, 2008 3:24 pm
Current Project: https://github.com/dbechrd/RicoTech
Favorite Gaming Platforms: NES, Sega Genesis, PS2, PC
Programming Language of Choice: C
Location: San Francisco
Contact:

Re: Dilemma with SDL screen surface

Post by dandymcgee »

lotios611 wrote:You don't even need to have an SDL_Surface for the screen. All you have to do is when you initialize the screen, do "SDL_SetVideoMode(args);" then when you want access to the screen, you do "SDL_GetVideoSurface();"
It's much more efficient the store a pointer to the screen surface than to query SDL for it every single time you want to render something...
Falco Girgis wrote:It is imperative that I can broadcast my narcissistic commit strings to the Twitter! Tweet Tweet, bitches! :twisted:
User avatar
Ginto8
ES Beta Backer
ES Beta Backer
Posts: 1064
Joined: Tue Jan 06, 2009 4:12 pm
Programming Language of Choice: C/C++, Java

Re: Dilemma with SDL screen surface

Post by Ginto8 »

dandymcgee wrote:
lotios611 wrote:You don't even need to have an SDL_Surface for the screen. All you have to do is when you initialize the screen, do "SDL_SetVideoMode(args);" then when you want access to the screen, you do "SDL_GetVideoSurface();"
It's much more efficient the store a pointer to the screen surface than to query SDL for it every single time you want to render something...
Well, even though global vars are bad, you could store the screen surface as a global var in a single source file. This source file will have all the necessary functions to render to said screen surface, without giving direct access. It's sorta like a singleton... but not quite. ;)
Quit procrastinating and make something awesome.
Ducky wrote:Give a man some wood, he'll be warm for the night. Put him on fire and he'll be warm for the rest of his life.
User avatar
dandymcgee
ES Beta Backer
ES Beta Backer
Posts: 4709
Joined: Tue Apr 29, 2008 3:24 pm
Current Project: https://github.com/dbechrd/RicoTech
Favorite Gaming Platforms: NES, Sega Genesis, PS2, PC
Programming Language of Choice: C
Location: San Francisco
Contact:

Re: Dilemma with SDL screen surface

Post by dandymcgee »

Ginto8 wrote:
dandymcgee wrote:
lotios611 wrote:You don't even need to have an SDL_Surface for the screen. All you have to do is when you initialize the screen, do "SDL_SetVideoMode(args);" then when you want access to the screen, you do "SDL_GetVideoSurface();"
It's much more efficient the store a pointer to the screen surface than to query SDL for it every single time you want to render something...
Well, even though global vars are bad, you could store the screen surface as a global var in a single source file. This source file will have all the necessary functions to render to said screen surface, without giving direct access. It's sorta like a singleton... but not quite. ;)
I've tossed it into my singleton 'Renderer' class in the past.
Falco Girgis wrote:It is imperative that I can broadcast my narcissistic commit strings to the Twitter! Tweet Tweet, bitches! :twisted:
XianForce
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 767
Joined: Wed Oct 29, 2008 8:36 pm

Re: Dilemma with SDL screen surface

Post by XianForce »

I I just have a renderer named VideoSystem, and the Window is stored in that. I never have need to access the window directly, because everything is rendered through the VideoSystem, and any events that affect it are passed straight to the window by my InputSystem...
User avatar
hurstshifter
ES Beta Backer
ES Beta Backer
Posts: 713
Joined: Mon Jun 08, 2009 8:33 pm
Favorite Gaming Platforms: SNES
Programming Language of Choice: C/++
Location: Boston, MA
Contact:

Re: Dilemma with SDL screen surface

Post by hurstshifter »

Lots of good stuff here guys. Thanks for the helpful comments.
"Time is an illusion. Lunchtime, doubly so."
http://www.thenerdnight.com
Post Reply