• Advertisement
Sign in to follow this  

Window Freezing When Drawing

This topic is 1722 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

So I'm currently drawing to the screen from a sprite on a 15x15 block sprite sheet with 16x16 blocks. Everytime I try to draw from here it freezes the window in debug. I'm using SDL...

 

#include "SDL.h"
#include "SDL_image.h"
#include "SDL_ttf.h"

void Apply_Surface(int x, int y, SDL_Surface* source, SDL_Surface* destination, SDL_Rect* clip = NULL){
	//Holds offsets
	SDL_Rect offset;

	//get offsets
	offset.x = x;
	offset.y = y;

	//Blit
	SDL_BlitSurface(source, clip, destination, &offset);
}

int main(int argc, char* args[]){
	//booleans
	bool running = false;
	bool mainMenu = true;
	
	//SDL Start
	if (SDL_Init(SDL_INIT_EVERYTHING) == -1){
		mainMenu = false;
		running = false;
	}

	//SDL_ttf Start
	if (TTF_Init() == -1){
		mainMenu = false;
		running = false;
	}

	//Image Declarations
	SDL_Surface* screen;
	SDL_Surface* menuBackground;
	SDL_Surface* sprites;
	SDL_Surface* mainBackground;

	//Images and Screens
	screen = SDL_SetVideoMode(800, 600, 32, SDL_SWSURFACE);
	menuBackground = IMG_Load("title.png");
	sprites = IMG_Load("Sprite Sheet 001.png");
	mainBackground = IMG_Load("Background.png");

	//Main Event
	SDL_Event e;

	//Window Caption
	SDL_WM_SetCaption("GDA: Casualties of War", NULL);

	//Sprite Sheet Clip Ranges
	SDL_Rect clip[225];
	//Start button
	clip[0].x = 0;
	clip[0].y = 0;
	clip[0].w = 16 * 9;
	clip[0].h = 16 * 2;

	//End Button
	clip[1].x = 0;
	clip[1].y = 16 * 2;
	clip[1].w = 16 * 4;
	clip[1].h = 16 * 2;

	//Start Button Label
	clip[2].x = 16 * 4;
	clip[2].y = 16 * 2;
	clip[2].w = 16 * 2;
	clip[2].h = 16 * 2;

	//End Button Label
	clip[3].x = 16 * 6;
	clip[3].y = 16 * 2;
	clip[3].w = 16 * 2;
	clip[3].h = 16 * 2;

	//Path
	clip[4].x = 16 * 22;
	clip[4].y = 16 * 4;
	clip[4].w = 16 * 4;
	clip[4].h = 16 * 4;

	//Main Menu Loop
	while(mainMenu){
		//Event Handling
		SDL_PollEvent(&e);
		
		if(e.type == SDL_QUIT){
			mainMenu = false;
			running = false;
		}

		if(e.type = SDL_KEYDOWN){
			switch(e.key.keysym.sym){
			case SDLK_s:
				running = true;
				SDL_FreeSurface(sprites);
				SDL_FreeSurface(menuBackground);
				while (running){
					//events
					SDL_PollEvent(&e);

					if(e.type == SDL_QUIT){
						running = false;
						mainMenu = false;
						SDL_Quit();
					}

					//draw
					

					Apply_Surface(0, 0, mainBackground, screen);
					Apply_Surface(300, 50, sprites, screen, &clip[4]); //THIS LINE MAKES IT FREEZE

					//Flip
					SDL_Flip(screen);
				}
			case SDLK_e:
				mainMenu = false;
				running = false;
			}
		}

		//Drawing
		SDL_FillRect(screen, NULL, 0);

		SDL_BlitSurface(menuBackground, NULL, screen, NULL);

		Apply_Surface(10, 590 - (16 * 2) - (16 * 2) - 10, sprites, screen, &clip[0]);
		Apply_Surface(47, 590 - (16 * 2), sprites, screen, &clip[1]);
		Apply_Surface(20 + (16 * 9), 590 - (16 * 2) - (16 * 2) - 10, sprites, screen, &clip[2]);
		Apply_Surface(20 + (16 * 9), 590 - (16 * 2), sprites, screen, &clip[3]);

		//Flip Screen
		SDL_Flip(screen);
	}

	//SDL_Stop
	SDL_Quit();

	return 0;
}

 

Share this post


Link to post
Share on other sites
Advertisement
You should be using == instead of = on this line.
if(e.type = SDL_KEYDOWN){

When the s key is pressed you free the surfaces sprites and menuBackground but a few lines later you are trying to use them. That is a problem.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement