Skip to content

Instantly share code, notes, and snippets.

@blister
Created April 13, 2010 14:25
Show Gist options
  • Save blister/364687 to your computer and use it in GitHub Desktop.
Save blister/364687 to your computer and use it in GitHub Desktop.
Base code for the Eric Harrison Game Development Project
#include "SDL/SDL.h"
#include "SDL/SDL_image.h"
#include "SDL/SDL_ttf.h"
#include <string>
// set up my drawing window
const int SCREEN_WIDTH = 1024;
const int SCREEN_HEIGHT = 768;
const int SCREEN_BPP = 32;
const int CONSOLE_WIDTH = 200;
const int CONSOLE_HEIGHT = 768;
const char *FONT_TTF = "cour.ttf";
// declare primary SDL surfaces that we'll be using
SDL_Surface *screen = NULL;
SDL_Surface *background = NULL;
SDL_Surface *console = NULL;
SDL_Surface *message = NULL;
// define our event
SDL_Event event;
// font declaration
TTF_Font *font = NULL;
SDL_Color textColor = { 255, 255, 255 };
// function to load an image (of any type) and return an optimzed SDL_Surface
SDL_Surface *load_image( std::string filename ) {
// current image
SDL_Surface* loadedImage = NULL;
//The optimized image that will be used
SDL_Surface* optimizedImage = NULL;
//Load the image
loadedImage = IMG_Load( filename.c_str() );
if( loadedImage != NULL ) {
optimizedImage = SDL_DisplayFormat( loadedImage );
// free up our resource
SDL_FreeSurface( loadedImage );
}
return optimizedImage;
}
void apply_surface( int x, int y, SDL_Surface* source, SDL_Surface* destination ) {
// offset is a temporary rect to hold the coordinates
SDL_Rect offset;
offset.x = x;
offset.y = y;
SDL_BlitSurface( source, NULL, destination, &offset );
}
bool init() {
if( SDL_Init( SDL_INIT_EVERYTHING ) == -1 ) {
return 1;
}
// prepare our screen
screen = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_SWSURFACE);
if( screen == NULL ) {
printf("The screen could not initialize. Exiting.\n");
return false;
}
if ( TTF_Init() == -1 ) {
printf("The TTF could not initialize. Exiting.\n");
return false;
}
// titlebar set
SDL_WM_SetCaption("The Game Project", NULL);
// all is well...
return true;
}
bool load_backgrounds() {
background = load_image("stars.jpg");
if( background == NULL ) {
return false;
}
console = load_image("console.png");
if ( console == NULL ) {
return false;
}
font = TTF_OpenFont(FONT_TTF, 20);
return true;
}
void debug(int x, int y) {
char cstr[50];
sprintf(cstr, "x:%i,y:%i", x,y);
message = TTF_RenderText_Solid(font, cstr, textColor);
apply_surface(SCREEN_WIDTH - CONSOLE_WIDTH, 0, console, screen);
apply_surface(SCREEN_WIDTH - CONSOLE_WIDTH + 10, 10, message,screen);
}
void clean_up() {
SDL_FreeSurface(background);
SDL_FreeSurface(console);
SDL_FreeSurface(message);
TTF_CloseFont(font);
TTF_Quit();
SDL_Quit();
}
int main( int argc, char* args[] ) {
bool quit = false;
int y = 0;
int x = 0;
if( init() == false ) {
return 1;
}
if( load_backgrounds() == false ) {
return 1;
}
// apply our background
apply_surface(0, 0, background, screen);
if( SDL_Flip(screen) == -1 ) {
return 1;
}
// main loop -- continue until SDL_QUIT is recieved
while(quit == false) {
while( SDL_PollEvent(&event) ) {
switch(event.type) {
case SDL_MOUSEMOTION:
x = event.motion.x;
y = event.motion.y;
debug(x,y);
SDL_Flip(screen);
break;
case SDL_QUIT:
printf("Exit!\n");
quit = true;
break;
default:
// do nothing
break;
}
}
}
// make sure we release our resources and exit SDL
clean_up();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment