News

Log In Sponsors
Partner Sites

CPP / SDL ---- Loads screen but everything is red



[b:1c1jat44] Color Code: Block 0 = red Block 1 = blue Block 2 = yellow Block 3 = green [/b:1c1jat44] [i:1c1jat44]#include "SDL/SDL.h" #include "SDL/SDL_image.h" #include <string> //Screen Attributes const int SCREEN_WIDTH = 600; const int SCREEN_HEIGHT = 400; const int SCREEN_BPP = 32; //Amount of Blocks const int VBLOCKS = 16; const int HBLOCKS = 24; //The Surfaces SDL_Surface *image = NULL; SDL_Surface *screen = NULL; SDL_Surface *example = NULL; //The Event Structure That Will Be Used SDL_Event event; //The portions of the sprite map to be blitted SDL_Rect clip[4]; SDL_Surface *load_image(std::string filename) { //The Image That's Loaded SDL_Surface* loadedImage = NULL; //The optimized image that will be used SDL_Surface* optimizedImage = NULL; //Load the image loadedImage = SDL_LoadBMP(filename.c_str()); if(loadedImage != NULL){ //Create an optimized image optimizedImage = SDL_DisplayFormat(loadedImage); //Free the old image SDL_FreeSurface(loadedImage); } //Return the optimized image return optimizedImage; } void apply_surface(int x, int y, SDL_Surface* source, SDL_Surface* destination, SDL_Rect* example = NULL) { //Temporary rectangle to hold the offsets SDL_Rect offset; //Get the offsets offset.x = x; offset.y = y; //Blit the surface SDL_BlitSurface(source, clip, destination, &offset); } bool init() { //Initialize all SDL subsystens if(SDL_Init(SDL_INIT_EVERYTHING) == -1) return false; //Setup the screen screen = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_SWSURFACE); //if there was an error in setting up the screen if(screen == NULL) return false; //Set the window caption SDL_WM_SetCaption("Metaroid", NULL); //If everything initialized fine return true; } bool load_files() { //Load the image image = load_image("images/basic/background.bmp"); example = load_image("images/basic/4square.bmp"); //if there was an error in loading the image if(image == NULL) return false; //if everything loaded fine return true; } void clean_up() { SDL_FreeSurface(image); SDL_FreeSurface(example); SDL_Quit(); } int main(int argc, char* args[]) { int i = 0, j = 0; int mapcoloring[VBLOCKS][HBLOCKS] = {{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}, {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}, {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}, {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}, {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}, {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}, {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}, {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}, {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}, {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}, {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}, {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}, {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}, {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}, {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}, {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}}; //Make sure the program waits for a quit bool quit = false; //initialize if(init() == false) return 1; //load the files if(load_files() == false) return 1; //Clip range for the top left clip[0].x = 0; clip[0].y = 0; clip[0].w = 25; clip[0].h = 25; //Clip range for the top right clip[1].x = 25; clip[1].y = 0; clip[1].w = 25; clip[1].h = 25; //Clip range for the bottom left clip[2].x = 0; clip[2].y = 25; clip[2].w = 25; clip[2].h = 25; //Clip range for the bottom right clip[3].x = 25; clip[3].y = 25; clip[3].w = 25; clip[3].h = 25; //While the user hasn't quit while(quit == false) { //Apply the surfaces to the screen apply_surface(0, 0, image, screen); for (i = 0; i < VBLOCKS; i++) { for (j = 0; j < HBLOCKS; j++) { apply_surface((j*25), (i*25), example, screen, &clip[mapcoloring[j][i]]); } } //update the screen if(SDL_Flip(screen) == -1) return 1; i = 0; j = 0; //while there's an event to handle while(SDL_PollEvent(&event)) { //If the user has Xed out the window if(event.type == SDL_QUIT) quit = true; } } //Free the surface and quit SDL clean_up(); return 0; } [/i:1c1jat44]

Click here to read the whole forum topic






cron