Alright, so, yeah, just trying my hand at SDL, and I know how to Blit surfaces, but I'm trying to right a functions to streamline it all. For some reason though, the image isn't showing up, so, if someone would like to enlighten me, that'd be great.
main.cpp
#include "sdl.h"
#include "main_functions.h"
int main(int argc, char* argv[])
{
main_functions mainfunct;
mainfunct.execute();
return 0;
}
main_functions.h
#ifndef MAIN_FUNCTIONS_H_
#define MAIN_FUNCTIONS_H_
#include "sdl.h"
#include "graphic_functions.h"
class main_functions
{
private:
bool running;
SDL_Event Event;
public:
SDL_Surface* screen;
public:
main_functions();
void execute();
bool start();
void check_event(SDL_Event Event);
void draw();
void clean_up();
};
#endif
main_functions.cpp
#include "main_functions.h"
main_functions::main_functions()
{
running = true;
screen = NULL;
}
bool main_functions::start()
{
if((SDL_Init(SDL_INIT_EVERYTHING)) < 0)
{
return false;
}
if((screen = SDL_SetVideoMode(600,400,16, SDL_HWSURFACE | SDL_DOUBLEBUF)) == NULL)
{
return false;
}
return true;
}
void main_functions::check_event(SDL_Event Event)
{
graphic_functions graphfunct;
if(SDL_PollEvent(&Event))
{
if(Event.type == SDL_KEYDOWN)
{
switch(Event.key.keysym.sym)
{
case SDLK_UP: graphfunct.subtracty();break;
case SDLK_DOWN: graphfunct.addy();break;
case SDLK_LEFT: graphfunct.subtractx(); break;
case SDLK_RIGHT: graphfunct.addx();break;
}
}
else if(Event.type == SDL_QUIT)
{
running = false;
}
}
}
void main_functions::draw()
{
graphic_functions graphfunct;
graphfunct.prepare_image("avy.bmp");
graphfunct.draw_sprite(graphfunct.avy,graphfunct.getx(),graphfunct.gety());
}
void main_functions::clean_up()
{
SDL_FreeSurface(screen);
}
execute.cpp
#include "main_functions.h"
void main_functions::execute()
{
start();
while(running)
{
check_event(Event);
draw();
SDL_Flip(SDL_GetVideoSurface());
}
clean_up();
}
graphic_functions.h
#ifndef GRAPHIC_FUNCTIONS_H_
#define GRAPHIC_FUNCTIONS_H_
#include "sdl.h"
#include "main_functions.h"
class graphic_functions
{
private:
int playerx;
int playery;
public:
//surfaces for optimization
SDL_Surface* original_image;
SDL_Surface* optimized_image;
//actual game graphics
SDL_Surface* avy;
graphic_functions();
void load_image(char* file);
void prepare_image(char* file);
void draw_sprite(SDL_Surface* sprite, int x, int y);
void moveplayer(SDL_Event Event);
int getx();
int gety();
void addx();
void subtractx();
void addy();
void subtracty();
};
#endif
graphic_functions.cpp
#include "graphic_functions.h"
graphic_functions::graphic_functions()
{
SDL_Surface* original_image = NULL;
SDL_Surface* optimized_image = NULL;
SDL_Surface* avy = NULL;
playerx = 1;
playery = 1;
}
void graphic_functions::load_image(char* file)
{
original_image = SDL_LoadBMP(file);
}
void graphic_functions::prepare_image(char* file)
{
load_image(file);
optimized_image = SDL_DisplayFormat(original_image);
avy = optimized_image;
}
void graphic_functions::draw_sprite(SDL_Surface* sprite, int x, int y)
{
main_functions mainfunct;
SDL_Rect rect;
rect.x = x;
rect.y = y;
SDL_BlitSurface(sprite,NULL,SDL_GetVideoSurface(),&rect);
}
int graphic_functions::getx()
{
return playerx;
}
int graphic_functions::gety()
{
return playery;
}
void graphic_functions::addx()
{
playerx++;
}
void graphic_functions::addy()
{
playery++;
}
void graphic_functions::subtractx()
{
playerx--;
}
void graphic_functions::subtracty()
{
playery--;
}
So, anybody know what's up?
[Edited by - Last Hylian on September 10, 2009 8:12:57 PM]