sdl.c
changeset 193 0a7025304867
parent 179 003096efeb9d
child 196 c4c33c0764a0
equal deleted inserted replaced
192:614bba52258d 193:0a7025304867
    21 
    21 
    22 #define MAX_DIRTY_RECTS 100
    22 #define MAX_DIRTY_RECTS 100
    23 static SDL_Rect _dirty_rects[MAX_DIRTY_RECTS];
    23 static SDL_Rect _dirty_rects[MAX_DIRTY_RECTS];
    24 static int _num_dirty_rects;
    24 static int _num_dirty_rects;
    25 
    25 
    26 #define SDL_CALL    
    26 #define SDL_CALL
    27 
    27 
    28 #if defined(DYNAMICALLY_LOADED_SDL) && defined(WIN32)
    28 #if defined(DYNAMICALLY_LOADED_SDL) && defined(WIN32)
    29 
    29 
    30 bool LoadLibraryList(void **proc, const char *dll);
    30 bool LoadLibraryList(void **proc, const char *dll);
    31 
    31 
    58 	SDL_Rect **(SDLCALL *SDL_ListModes)(void *, int);
    58 	SDL_Rect **(SDLCALL *SDL_ListModes)(void *, int);
    59 	Uint8 *(SDLCALL *SDL_GetKeyState)(int *);
    59 	Uint8 *(SDLCALL *SDL_GetKeyState)(int *);
    60 } SDLProcs;
    60 } SDLProcs;
    61 
    61 
    62 #define M(x) x "\0"
    62 #define M(x) x "\0"
    63 static const char sdl_files[] = 
    63 static const char sdl_files[] =
    64 	M("sdl.dll")
    64 	M("sdl.dll")
    65 	M("SDL_Init")
    65 	M("SDL_Init")
    66 	M("SDL_InitSubSystem")
    66 	M("SDL_InitSubSystem")
    67 	M("SDL_GetError")
    67 	M("SDL_GetError")
    68 	M("SDL_QuitSubSystem")
    68 	M("SDL_QuitSubSystem")
    95 
    95 
    96 static SDLProcs _proc;
    96 static SDLProcs _proc;
    97 
    97 
    98 static char *LoadSdlDLL()
    98 static char *LoadSdlDLL()
    99 {
    99 {
   100 	if (_proc.SDL_Init != NULL) 
   100 	if (_proc.SDL_Init != NULL)
   101 		return NULL;
   101 		return NULL;
   102 	if (!LoadLibraryList((void**)&_proc, sdl_files))
   102 	if (!LoadLibraryList((void**)&_proc, sdl_files))
   103 		return "Unable to load sdl.dll";
   103 		return "Unable to load sdl.dll";
   104 	return NULL;
   104 	return NULL;
   105 }
   105 }
   245 	} else {
   245 	} else {
   246 		int n = 0;
   246 		int n = 0;
   247 		for(i = 0; modes[i]; i++) {
   247 		for(i = 0; modes[i]; i++) {
   248 			int w = modes[i]->w;
   248 			int w = modes[i]->w;
   249 			int h = modes[i]->h;
   249 			int h = modes[i]->h;
   250 			if (IS_INT_INSIDE(w, 640, MAX_SCREEN_WIDTH+1) && 
   250 			if (IS_INT_INSIDE(w, 640, MAX_SCREEN_WIDTH+1) &&
   251 					IS_INT_INSIDE(h, 480, MAX_SCREEN_HEIGHT+1) &&
   251 					IS_INT_INSIDE(h, 480, MAX_SCREEN_HEIGHT+1) &&
   252 					w%8 == 0 && h%8 == 0) { // disable screen resolutions which are not multiples of 8
   252 					w%8 == 0 && h%8 == 0) { // disable screen resolutions which are not multiples of 8
   253 				int j;
   253 				int j;
   254 				for (j = 0; j < n; ++j)
   254 				for (j = 0; j < n; ++j)
   255 					if (_resolutions[j][0] == w && _resolutions[j][1] == h)
   255 					if (_resolutions[j][0] == w && _resolutions[j][1] == h)
   361 	AS(SDLK_KP_MULTIPLY,	WKC_NUM_MUL),
   361 	AS(SDLK_KP_MULTIPLY,	WKC_NUM_MUL),
   362 	AS(SDLK_KP_MINUS,			WKC_NUM_MINUS),
   362 	AS(SDLK_KP_MINUS,			WKC_NUM_MINUS),
   363 	AS(SDLK_KP_PLUS,			WKC_NUM_PLUS),
   363 	AS(SDLK_KP_PLUS,			WKC_NUM_PLUS),
   364 	AS(SDLK_KP_ENTER,			WKC_NUM_ENTER),
   364 	AS(SDLK_KP_ENTER,			WKC_NUM_ENTER),
   365 	AS(SDLK_KP_PERIOD,		WKC_NUM_DECIMAL),
   365 	AS(SDLK_KP_PERIOD,		WKC_NUM_DECIMAL),
   366 	{0, 0, 0}	
   366 	{0, 0, 0}
   367 };
   367 };
   368 
   368 
   369 static uint32 ConvertSdlKeyIntoMy(SDL_keysym *sym)
   369 static uint32 ConvertSdlKeyIntoMy(SDL_keysym *sym)
   370 {
   370 {
   371 	const VkMapping	*map = _vk_mapping - 1;
   371 	const VkMapping	*map = _vk_mapping - 1;
   374 	for(;;) {
   374 	for(;;) {
   375 		map++;
   375 		map++;
   376 		from = map->vk_from;
   376 		from = map->vk_from;
   377 		if (from == 0) {
   377 		if (from == 0) {
   378 			key = 0;
   378 			key = 0;
   379 			break;		
   379 			break;
   380 		}
   380 		}
   381 		if ((uint)(key - from) <= map->vk_count) {
   381 		if ((uint)(key - from) <= map->vk_count) {
   382 			key = key - from + map->map_to;
   382 			key = key - from + map->map_to;
   383 			break;
   383 			break;
   384 		}
   384 		}
   442 			_left_button_down = true;
   442 			_left_button_down = true;
   443 		} else if (ev.button.button == SDL_BUTTON_RIGHT) {
   443 		} else if (ev.button.button == SDL_BUTTON_RIGHT) {
   444 			_right_button_down = true;
   444 			_right_button_down = true;
   445 			_right_button_clicked = true;
   445 			_right_button_clicked = true;
   446 		}
   446 		}
   447 #if !defined(WIN32)		
   447 #if !defined(WIN32)
   448 		 else if (ev.button.button == SDL_BUTTON_WHEELUP) {
   448 		 else if (ev.button.button == SDL_BUTTON_WHEELUP) {
   449 			_cursor.wheel--;
   449 			_cursor.wheel--;
   450 		} else if (ev.button.button == SDL_BUTTON_WHEELDOWN) {
   450 		} else if (ev.button.button == SDL_BUTTON_WHEELDOWN) {
   451 			_cursor.wheel++;
   451 			_cursor.wheel++;
   452 		}
   452 		}
   477 			MarkWholeScreenDirty();
   477 			MarkWholeScreenDirty();
   478 		} else {
   478 		} else {
   479 			_pressed_key = ConvertSdlKeyIntoMy(&ev.key.keysym);
   479 			_pressed_key = ConvertSdlKeyIntoMy(&ev.key.keysym);
   480 		}
   480 		}
   481 		break;
   481 		break;
   482 		
   482 
   483 	case SDL_VIDEORESIZE: {
   483 	case SDL_VIDEORESIZE: {
   484 			int w, h;
   484 			int w, h;
   485 			w = ev.resize.w;
   485 			w = ev.resize.w;
   486 			h = ev.resize.h;
   486 			h = ev.resize.h;
   487 			
   487 
   488 			w = clamp(w & ~0x7, 64, MAX_SCREEN_WIDTH);
   488 			w = clamp(w & ~0x7, 64, MAX_SCREEN_WIDTH);
   489 			h = clamp(h & ~0x7, 64, MAX_SCREEN_HEIGHT);
   489 			h = clamp(h & ~0x7, 64, MAX_SCREEN_HEIGHT);
   490 			
   490 
   491 			ChangeResInGame(w, h);
   491 			ChangeResInGame(w, h);
   492 			
   492 
   493 			break;
   493 			break;
   494 		}
   494 		}
   495 	}
   495 	}
   496 	return -1;
   496 	return -1;
   497 }
   497 }
   527 	int numkeys;
   527 	int numkeys;
   528 	Uint8 *keys;
   528 	Uint8 *keys;
   529 
   529 
   530 	while (true) {
   530 	while (true) {
   531 		InteractiveRandom(); // randomness
   531 		InteractiveRandom(); // randomness
   532 		
   532 
   533 		while ((i=PollEvent()) == -1) {}
   533 		while ((i=PollEvent()) == -1) {}
   534 		if (i>=0) return i;
   534 		if (i>=0) return i;
   535 
   535 
   536 		if (_exit_game)	return ML_QUIT;
   536 		if (_exit_game)	return ML_QUIT;
   537 
   537 
   538 		mod = SDL_CALL SDL_GetModState();
   538 		mod = SDL_CALL SDL_GetModState();
   539 		keys = SDL_CALL SDL_GetKeyState(&numkeys);		
   539 		keys = SDL_CALL SDL_GetKeyState(&numkeys);
   540 #if defined(_DEBUG)
   540 #if defined(_DEBUG)
   541 		if (_shift_pressed) {
   541 		if (_shift_pressed) {
   542 #else
   542 #else
   543 		if (keys[SDLK_TAB]) {
   543 		if (keys[SDLK_TAB]) {
   544 #endif
   544 #endif
   554 		if (cur_ticks == next_tick) {
   554 		if (cur_ticks == next_tick) {
   555 			next_tick += 30;
   555 			next_tick += 30;
   556 
   556 
   557 			_ctrl_pressed = !!(mod & (KMOD_LCTRL | KMOD_RCTRL));
   557 			_ctrl_pressed = !!(mod & (KMOD_LCTRL | KMOD_RCTRL));
   558 			_shift_pressed = !!(mod & (KMOD_LSHIFT | KMOD_RSHIFT));
   558 			_shift_pressed = !!(mod & (KMOD_LSHIFT | KMOD_RSHIFT));
   559 			
   559 
   560 			// determine which directional keys are down
   560 			// determine which directional keys are down
   561 			_dirkeys = 
   561 			_dirkeys =
   562 			    (keys[SDLK_LEFT] ? 1 : 0) + 
   562 			    (keys[SDLK_LEFT] ? 1 : 0) +
   563 			    (keys[SDLK_UP] ? 2 : 0) + 
   563 			    (keys[SDLK_UP] ? 2 : 0) +
   564 			    (keys[SDLK_RIGHT] ? 4 : 0) + 
   564 			    (keys[SDLK_RIGHT] ? 4 : 0) +
   565 			    (keys[SDLK_DOWN] ? 8 : 0);
   565 			    (keys[SDLK_DOWN] ? 8 : 0);
   566 			GameLoop();
   566 			GameLoop();
   567 
   567 
   568 			_screen.dst_ptr = _sdl_screen->pixels;
   568 			_screen.dst_ptr = _sdl_screen->pixels;
   569 			UpdateWindows();
   569 			UpdateWindows();
   605 }
   605 }
   606 
   606 
   607 static char *SdlSoundStart(char **parm)
   607 static char *SdlSoundStart(char **parm)
   608 {
   608 {
   609 	SDL_AudioSpec spec;
   609 	SDL_AudioSpec spec;
   610 	
   610 
   611 	{char *s;if ((s = SdlOpen(SDL_INIT_AUDIO)) != NULL) return s;}
   611 	{char *s;if ((s = SdlOpen(SDL_INIT_AUDIO)) != NULL) return s;}
   612 	spec.freq = GetDriverParamInt(parm, "hz", 11025);
   612 	spec.freq = GetDriverParamInt(parm, "hz", 11025);
   613 	spec.format = AUDIO_S16SYS;
   613 	spec.format = AUDIO_S16SYS;
   614 	spec.channels = 2;
   614 	spec.channels = 2;
   615 	spec.samples = 512;
   615 	spec.samples = 512;