Jump to content

  • Log In with Google      Sign In   
  • Create Account

FREE SOFTWARE GIVEAWAY

We have 4 x Pro Licences (valued at $59 each) for 2d modular animation software Spriter to give away in this Thursday's GDNet Direct email newsletter.


Read more in this forum topic or make sure you're signed up (from the right-hand sidebar on the homepage) and read Thursday's newsletter to get in the running!


Window Freezing When Drawing


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
1 reply to this topic

#1 Galactic Development Arts   Members   -  Reputation: 104

Like
0Likes
Like

Posted 04 May 2013 - 04:47 PM

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

 



Sponsor:

#2 Wooh   Members   -  Reputation: 653

Like
2Likes
Like

Posted 04 May 2013 - 06:29 PM

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.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS