Advertisement

Would you look at my engine please? (C++ and SDL)

Started by December 23, 2010 05:16 PM
4 comments, last by BCBTP 13 years, 2 months ago
Okay, so I know I haven't spend enough time learning C++ but I tried writing a 2D game engine in SDL.
The point is, I've looked at other game engines and their code looks really different. For instance, I don't use classes, my engine consists solely of headers containing the functions I need.

My question to you is, am I doing something wrong and if so, what am I doing wrong?

Also, if someone has experience with Linux programming, am I doing the header thing right, because I'm not sure it will compile on Linux (it hasn't been tested yet).

The engine is not even nearly finished, it's just the graphics part (it loads images and text).

Anyway, here's my code, please look at it:
Visual Studio 2010 version (Windows, maybe not up to date)
XCode version (Mac OS-X)
How much code is there? Any chance you could just post some or all of it here (using 'source' tags, of course)? You might get more people to check it out that way.

(Obviously if there's a great deal of code that might not work so well, but if there's too much code, people probably aren't going to be that inclined to look through it anyway.)
Advertisement
main.cpp
;//Include the dependency headers per Operating System#ifdef APPLE || defined (__linux__)#include "SDL/SDL.h"#include "SDL_ttf/SDL_ttf.h"#elif defined (_WIN32)#include "SDL.h"#include "SDL_ttf.h"#include #endif//Include the other headers#include "Engine/Engine.h"TTF_Font *font = NULL;SDL_Color textcolour =  {255, 255, 255};int main(int argc, char* args[]){		//Define the image and the screen and set them to NULL	SDL_Surface* image = NULL;	SDL_Surface* screen = NULL;	SDL_Surface* bowser = NULL;	SDL_Surface* message = NULL;		init();		//Set up screen	screen = SDL_SetVideoMode(800, 600, 32, SDL_SWSURFACE);	//Load images	image = load_image("resources/images/Henk.png", screen, true);		//Open our font	font = loadfont("resources/fonts/MORPHEUS.TTF", 30);	if (font == NULL){		return 1;	}		//Render our text	message = TTF_RenderText_Blended(font, "The quick brown fox jumps over the lazy dog", textcolour);	if (message == NULL){		return 1;	}	blit(100,100,message, screen);	//Update screen	SDL_Flip(screen);		//Pause	SDL_Delay(10000);	//Free our images and text	SDL_FreeSurface(image);	SDL_FreeSurface(bowser);	SDL_FreeSurface(message); 	//Close the font that was used 	TTF_CloseFont( font ); 	//Quit SDL_ttf 	TTF_Quit(); 	//Quit SDL	SDL_Quit();	return 0;}

/Engine/Engine.h
/* *  Engine.h *  Tutorials * *  Created by Robin van ee on 08/10/2010. * * This is the main engine file, it's only purpose is including all headers at once. *  */#include "Actions/Events.h"#include "Graphics/image.h"#include "Graphics/fonts.h"

/Engine/Graphics/image.h
/* *  image.h *  Tutorials * *  Created by Robin van ee on 08/10/2010. * * This is the class that handles loading images. * It can load BMP, PNM, XPM, LBM, PCX, GIF, JPEG, TGA and PNG files. * It also handles putting the images on the screen. */#include "SDL_image/SDL_image.h"#include <string>//Start image blitting function, automatically used by load_image()void blit( int x, int y, SDL_Surface* source, SDL_Surface* destination ) {	//Make a temporary rectangle to hold the offsets 	SDL_Rect offset; 	//Give the offsets to the rectangle 	offset.x = x; offset.y = y; 	//Blit the surface	SDL_BlitSurface( source, NULL, destination, &offset ); } //Start image loading functionSDL_Surface *load_image(std::string filename, SDL_Surface *screen, bool alpha = false){	//Temporary storage for the image	SDL_Surface* image = NULL;	//Make a surface named "tmp"	SDL_Surface *tmp;	//Load the actual image	image = IMG_Load(filename.c_str());		//If nothing went wrong while loading the image	if (image != NULL){			//If alpha is true			if (alpha) {				//tmp is image in alpha				tmp = ::SDL_DisplayFormatAlpha(image);			}			else {				tmp = ::SDL_DisplayFormat(image);			}			SDL_FreeSurface(image);	}	//return the optimized image	blit(0, 0, tmp, screen);	return tmp;}



I haven't posted all code, but I think that will leave an impression of how I'm working.
Just took a quick look, and what you have there seems pretty straightforward. It's not really idiomatic C++ (as you probably already know), and it's probably not really an 'engine' either, IMO, but that shouldn't concern you. The term 'engine' gets thrown around a lot, but what you should be most concerned with (IMO) is simply writing robust code that serves your purpose and is at least reasonably modular/reusable.

As for the overall design, some people prefer C-style procedural design (which is what you're doing, essentially), but IMO, there are some advantages to using object-oriented techniques and C++ idioms such as RAII. If you're coding it this way just because that's how you prefer to work, that's all good, but if you're coding it this way because you're unfamiliar with the alternatives, I'd recommend learning a little about OOP, RAII, and idiomatic C++, and seeing if taking a more object-oriented approach appeals to you at all.
Thanks for your reply.
No, I don't do this because I am unfamiliar with classes but this way just seemed easier to me and it works.

I will look into the advantage of true OOP, and probably rewrite the whole lot.

Thanks!
Yeah, I am kind of at the same stage you are in the engine, but I have used OOP.
This is how long my main file is.


#include "mh.h"
using namespace std;

int main(int argc, char* args[]){
bool quit = false;
nEngine->InitEngine(XAXE, YAXE, BPI, "Hello SDL");
nImage->SurfaceLoad("image.bmp", 0, 0, 0);
nImage->ApplySurface(0, 0, nEngine->screen);
if(SDL_Flip(nEngine->screen) == -1){
return 1;
}
while(quit == false){
while(SDL_PollEvent(&nEvent)){
if(nEvent.type == SDL_QUIT){
quit = true;
}
}
}
SDL_Quit();
delete nEngine;
delete nImage;
return 0;
}




It is less repetitive to do it this way, because in my case instead of writing [color=red][font=CourierNew, monospace]

SDL

[/font][font=CourierNew, monospace]_BlitSurface( image, NULL, screen, &offset ); [/font]
[font=CourierNew, monospace]I can go nImage->Blit(0, 0, nEngine->Screen); And even less if I make SDL_Surface *nScreen; part of Surface point to &screen of Engine class.[/font]
[font=CourierNew, monospace]
[/font]
[font=CourierNew, monospace]And if your wondering, most of my stuff is defined in my header or in objects.[/font]
[font=CourierNew, monospace]
[/font]
[font="CourierNew, monospace"]I am also very new to SDL(about a week), with about 1.5 years of C++.[/font]
[font="CourierNew, monospace"]OOP is better IMO because it is easier to make your main file, even if your other files are giant. [/font]
[font="CourierNew, monospace"]
[/font]
[font="CourierNew, monospace"]But, if you like C-style better, go right ahead![/font]

This topic is closed to new replies.

Advertisement