Hi people,
I’m developing an SDL console for colinux.
I have used the SDL library under win32.
The following program show well the problem I have encoutered.
There is :
main
- change window size
- check events
drawing thread
- contine to update window
I have used EnterCriticalSection and LeaveCriticalSection to avoid
race condition problem.
To see the problem run it and try some times to minimize and maximize.
After some proofs you will see that application does not respond anymore.
Any ideas ?
Do I make some error ?
Thanks
Paolo********************************************************************************
#include <stdio.h>
#include <SDL.h>
#include <windows.h>
#define WIDTH 640
#define HEIGHT 20
#define BPP 4
#define DEPTH 32
SDL_Surface *screen;
CRITICAL_SECTION crit;
unsigned char K=0;
void setpixel(SDL_Surface *screen, int x, int y, Uint8 r, Uint8 g, Uint8 b)
{
Uint32 pixmem32;
Uint32 colour;
colour = SDL_MapRGB( screen->format, r, g, b );
pixmem32 = (Uint32) screen->pixels + y + x;
*pixmem32 = colour;
}
void DrawScreen(SDL_Surface* screen, int h)
{
int x, y, ytimesw;
for(y = 0; y < screen->h; y++ )
{
ytimesw = yscreen->pitch/BPP;
for( x = 0; x < screen->w; x++ )
{
setpixel(screen, x, ytimesw, (xx)/256+3y+h, (yy)/256+x+h, h);
}
}
SDL_Flip(screen);
}
void drawing_thread(void *p)
{
static int h=0;
while(1)
{
EnterCriticalSection(&crit);
DrawScreen(screen,h++);
LeaveCriticalSection(&crit);
}
}
void Change_Window_Size()
{
EnterCriticalSection(&crit);
if (!(screen = SDL_SetVideoMode(WIDTH, HEIGHT+K, DEPTH, 0 )))
{
SDL_Quit();
printf(“ERRORE\n”);
fflush(stdout);
return;
}
LeaveCriticalSection(&crit);
}
int main(int argc, char* argv[])
{
SDL_Event event;
int keypress = 0;
InitializeCriticalSection(&crit);
if (SDL_Init(SDL_INIT_VIDEO) < 0 ) return 1;
if (!(screen = SDL_SetVideoMode(WIDTH, HEIGHT, DEPTH, 0 )))
{
SDL_Quit();
printf("ERRORE\n");
fflush(stdout);
return 1;
}
_beginthread(drawing_thread,0,0);
K=0;
while(!keypress)
{
K++;
if ( (K % 8) == 0 )
{
Change_Window_Size();
}
// Sleep(10);
while(SDL_PollEvent(&event))
{
switch (event.type)
{
case SDL_QUIT:
keypress = 1;
break;
case SDL_KEYDOWN:
keypress = 1;
break;
}
}
}
SDL_Quit();
return 0;
}