JOIN DEVPPL

It looks like you are new here. If you want to get involved, click on Join below!



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


I

keyboard_arrow_up
0
keyboard_arrow_down


First Post Mark as Spam instinct46
Posts: 92 - Registered: 2007-03-27 08:07:09


Color Code:
Block 0 = red
Block 1 = blue
Block 2 = yellow
Block 3 = green



#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;

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 = {{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.x = 0;
clip.y = 0;
clip.w = 25;
clip.h = 25;

//Clip range for the top right
clip.x = 25;
clip.y = 0;
clip.w = 25;
clip.h = 25;

//Clip range for the bottom left
clip.x = 0;
clip.y = 25;
clip.w = 25;
clip.h = 25;

//Clip range for the bottom right
clip.x = 25;
clip.y = 25;
clip.w = 25;
clip.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]);
}
}

//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

keyboard_arrow_up
0
keyboard_arrow_down


Reply #1 Mark as Spam instinct46
Posts: 92 - Registered: 2007-03-27 08:07:09

Thanks if you looked through the code, just encase anyone looks through still and can't find the problem, its to do with this line near the bottom

apply_surface((j*25), (i*25), example, screen, &clip]);






JOIN DEVPPL

It looks like you are new here. If you want to get involved, click on Join below!




MENU
Advertising