My graphics engine works if i comment out one line. If I uncomment it, it appears to work but it doesn''t. The window closes after the x is pressed, but it''s still in the ctrl-alt-del menu. I have to end the task before I can link my program. Do you see anything wrong in my code?
//graphics.h
#include <vector>
#include <string>
class graphics {
public:
graphics();
~graphics();
bool init(int, int, int);
int LoadSurface(std::string);
void FreeSurfaces();
int get_width();
int get_height();
int get_bpp();
void blit_tile(int, int, int, int, int, int, int);
void render_begin();
void render_end();
private:
vector surfaces;
SDL_Surface* screen;
int screen_width;
int screen_height;
int screen_bpp;
};
graphics::graphics() {
//initialize class
screen_width = 0;
screen_height = 0;
screen_bpp = 0;
}
graphics::~graphics() {
//free graphics
FreeSurfaces();
}
void graphics::FreeSurfaces() {
while(surfaces.size() > 0) {
SDL_FreeSurface(surfaces[surfaces.size() - 1]);
}
}
bool graphics::init(int width, int height, int bpp) {
if(SDL_Init(SDL_INIT_VIDEO) < 0) {
return false;
}
screen = SDL_SetVideoMode(width, height, bpp, SDL_SWSURFACE);
if(screen == NULL) {
return false;
}
screen_width = width;
screen_height = height;
screen_bpp = bpp;
return true;
}
int graphics::LoadSurface(std::string file) {
SDL_Surface* surface;
int index;
index = surfaces.size();
surface = SDL_LoadBMP(file.data());
if(surface == NULL) {
cerr << "could not load: " << file << endl << SDL_GetError() << endl;
return -1;
}
surfaces.push_back(surface);
SDL_FreeSurface(surface);
return index;
}
int graphics::get_width() {
return screen_width;
}
int graphics::get_height() {
return screen_height;
}
int graphics::get_bpp() {
return screen_bpp;
}
void graphics::blit_tile(int index, int tilex, int tiley, int srcx, int srcy, int width, int height) {
SDL_Rect dest;
SDL_Rect src;
dest.x = tilex * width;
dest.y = tiley * height;
dest.w = width;
dest.h = height;
src.x = srcx * width;
src.y = srcy * height;
src.w = width;
src.h = height;
SDL_BlitSurface(surfaces[index], &src, screen, &dest);
}
void graphics::render_begin() {
//fill a blank screen
SDL_FillRect(screen, 0, SDL_MapRGB(screen->format, 125, 0, 0));
}
void graphics::render_end() {
//update screen
SDL_UpdateRect(screen, 0, 0, 0, 0);
}
..
//main.cpp
#include "SDL.h"
#include "graphics.h"
graphics game_graphics;
bool bdone = false;
int main(int argc, char *argv[]) {
SDL_Event event;
int stileset;
//init
if(game_graphics.init(640, 480, 16)==false) {
cerr << "Error" << endl << SDL_GetError() << endl;
return -1;
}
//stileset = game_graphics.LoadSurface("tileset.bmp");
//this is the line that is a problem
do {
//events
while(SDL_PollEvent(&event)) {
switch(event.type) {
case SDL_QUIT:
bdone = true;
break;
}
}
//input
//math
//render
game_graphics.render_begin();
//game_graphics.blit_tile(stileset, 0, 0, 0, 0, 32, 32);
//going to blit, but can''t until surface problem is fixed
game_graphics.render_end();
}while(!bdone);
//done
SDL_Quit();
cout << "done." << endl;
return 0;
}