• Advertisement
Sign in to follow this  

Problem with SDL_Mix and mouse buttons

This topic is 3577 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

Hello, I've ran into this problem when trying to run SDL_Mix yesterday, since then I looked around alot, but I couldn't really find anything like my problem = Anyway, I'm making a Tetris Clone as a first project, and on the game screen I have a side menu with buttons to be operated by the mouse(Start, Pause, Music, Records, etc.), right now the only button initiallized for being clickable is the Start Button, that turns to a stop button(Using background flip) that closes the game, after being clicked and spawning a block on the screen, this button works perfectly and does everything correctly, but once I put in the SDL_Mixer and music/sound commands, whenever I click the button it makes the game close = I tried isolating the code and came to the conclusion that the problem causing part of the code is the Mix_OpenAudio command... here's the relevant code excerpt: Headers, initiallizers and all the other beginning crap. #include "stdafx.h" #include <iostream> #include <string> #include "SDL/SDL.h" #include "SDL/SDL_image.h" #include "SDL/SDL_mixer.h" #ifndef GRAPHIC #define GRAPHIC #include "GraphicsFunction.h" #endif SDL_Event event; const int WINDOW_WIDTH = 384; const int WINDOW_HEIGHT = 576; SDL_Surface *screen; SDL_Surface *background; SDL_Surface *sprite; Mix_Music *TTA; Mix_Chunk *moveblock; Mix_Chunk *placeblock; Mix_Chunk *destroyline; Mix_Chunk *special; Mix_Chunk *pause; //A function call to draw a sprite on screen. void drawSprite(SDL_Surface *string, SDL_Surface *screen, int srcx, int srcy, int srcw, int srch, double destx, double desty, int destw, int desth); int main(int argc, char *args[]) { SDL_Init(SDL_INIT_VIDEO|SDL_INIT_AUDIO); Mix_OpenAudio(22050, AUDIO_S16, 2, 4096); SDL_Surface* screen = SDL_SetVideoMode(WINDOW_WIDTH, WINDOW_HEIGHT, 0, SDL_DOUBLEBUF|SDL_HWSURFACE); SDL_WM_SetCaption("BattleBlocks", 0); //coordinates for the start button SDL_Rect start; start.x = 281; start.y = 186; start.w = 84; start.h = 33; menu loop: //main game loop, starts with menu and moves to 2nd game loop when start is pressed. bool keysHeld[323]; bool gamerunning=false; bool menu=true; while(menu) { if(SDL_PollEvent(&event)) { if(event.type == SDL_QUIT) { menu=false; gamerunning=false; } int x = 0; int y = 0; if(event.type == SDL_MOUSEBUTTONDOWN) { if(event.button.button == SDL_BUTTON_LEFT) { x = event.motion.x; y = event.motion.y; if((x>start.x)&&(x<(start.x+start.w))&&(y>start.y)&&(y<(start.y+start.h))) { //draw the block sprite , change the background image and start main game. drawSprite(sprite, screen, 0, 0, 37, 37, destx, desty, 37, 37); SDL_FreeSurface(background); SDL_Surface *background = IMG_Load("Backgroundwp.png"); gamerunning=true; }}}} SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format, 0, 0, 0)); drawSprite(background, screen, 0, 0, 384, 576, 0, 0, 384, 576); SDL_Flip(screen); *After this comes a 2nd loop for the game itself, but that is not important right now, the game closes before any of the code here gets to take effect* I don't get any errors while running, but 2 warnings about changing a double value into a Sint16 for the falling speed(1.1, 0.3 and 1.9) in the 2nd loop, however that never gave me any problems before the Mix_OpenAudio, so I doubt that's the reason..... Any help in this please?

Share this post


Link to post
Share on other sites
Advertisement
Looks like you're not checking return values/error codes. Be sure to check the return values of functions like SDL_Init() and MIX_OpenAudio(); you can also use (e.g.) SDL_GetError/Mix_GetError() (or whatever those functions are called) to see what went wrong if you do get an error.

Share this post


Link to post
Share on other sites
I haven't really heard of those anywhere...
Can you explain how they work(What arguments to use etc.)

Edit: Okay, I sorted it out with a few hints from the debugger =P
For future reference: apparently in this part of the code:

//draw the block sprite , change the background image and start main game.
drawSprite(sprite, screen, 0, 0, 37, 37, destx, desty, 37, 37);
SDL_FreeSurface(background);
SDL_Surface *background = IMG_Load("Backgroundwp.png");
gamerunning=true;
}}}}

When I changed the Surface to load to a different surface it stopped the crushing... no idea why, or even why the SDL_Mix caused it, but that's what fixed it =

Anyway, I would still like to know what the GetError comands do if you wouldn't mind taking the time to explain.
Thanks.

[Edited by - DestX on April 3, 2008 2:27:51 PM]

Share this post


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

  • Advertisement