/* sdloshw.c: Top-level SDL management functions. * * Copyright (C) 2001-2006 by Brian Raiter, under the GNU General Public * License. No warranty. See COPYING for details. * * Modified by Yogeshwar Velingker, 2013. */ #ifndef _TINSPIRE #include #include #include #include "SDL.h" #else #include #include #endif #include "sdlgen.h" #include "../err.h" #include "../defs.h" /* Values global to this library. */ oshwglobals sdlg; /* This is an automatically-generated file, which contains a * representation of the program's icon. */ #ifndef _TINSPIRE #include "ccicon.c" #endif /* Dispatch all events sitting in the SDL event queue. */ static void _eventupdate(int wait) { static int mouselastx = -1, mouselasty = -1; SDL_Event event; if (wait) SDL_WaitEvent(NULL); SDL_PumpEvents(); while (SDL_PeepEvents(&event, 1, SDL_GETEVENT, SDL_ALLEVENTS)) { switch (event.type) { case SDL_KEYDOWN: if (windowmappos(mouselastx, mouselasty) < 0) SDL_ShowCursor(SDL_DISABLE); keyeventcallback(event.key.keysym.sym, TRUE); if (event.key.keysym.unicode && event.key.keysym.unicode != event.key.keysym.sym) { keyeventcallback(event.key.keysym.unicode, TRUE); keyeventcallback(event.key.keysym.unicode, FALSE); } break; case SDL_KEYUP: if (windowmappos(mouselastx, mouselasty) < 0) SDL_ShowCursor(SDL_DISABLE); keyeventcallback(event.key.keysym.sym, FALSE); break; case SDL_MOUSEBUTTONDOWN: case SDL_MOUSEBUTTONUP: SDL_ShowCursor(SDL_ENABLE); mouselastx = event.motion.x; mouselasty = event.motion.y; mouseeventcallback(event.button.x, event.button.y, event.button.button, event.type == SDL_MOUSEBUTTONDOWN); break; case SDL_MOUSEMOTION: SDL_ShowCursor(SDL_ENABLE); mouselastx = event.motion.x; mouselasty = event.motion.y; break; case SDL_QUIT: oshwshutdown(); exit(EXIT_SUCCESS); } } } #ifndef _TINSPIRE /* Alter the window decoration. */ void setsubtitle(char const *subtitle) { char buf[270]; if (subtitle && *subtitle) { sprintf(buf, "Tile World - %.255s", subtitle); SDL_WM_SetCaption(buf, "Tile World"); } else { SDL_WM_SetCaption("Tile World", "Tile World"); } } #endif /* Shut down SDL. If using Nspire, shut down timer module first. */ #ifndef _TINSPIRE static void shutdown(void) #else void oshwshutdown(void) #endif { #ifdef _TINSPIRE sdltimershutdown(); #endif SDL_Quit(); } /* Initialize SDL, create the program's icon, and then initialize * the other modules of the library. */ int oshwinitialize(int silence, int soundbufsize, int showhistogram, int fullscreen) { #ifndef _TINSPIRE SDL_Surface *icon; #endif sdlg.eventupdatefunc = _eventupdate; if (SDL_Init(SDL_INIT_VIDEO) < 0) { errmsg(NULL, "Cannot initialize SDL system: %s\n", SDL_GetError()); return FALSE; } #ifndef _TINSPIRE atexit(shutdown); setsubtitle(NULL); icon = SDL_CreateRGBSurfaceFrom(cciconimage, CXCCICON, CYCCICON, 32, 4 * CXCCICON, 0x0000FF, 0x00FF00, 0xFF0000, 0); if (icon) { SDL_WM_SetIcon(icon, cciconmask); SDL_FreeSurface(icon); } else warn("couldn't create icon surface: %s", SDL_GetError()); #endif return _sdltimerinitialize(showhistogram) && _sdltextinitialize() && _sdltileinitialize() && _sdlinputinitialize() && _sdloutputinitialize(fullscreen) && _sdlsfxinitialize(silence, soundbufsize); } /* The real main(). */ int main(int argc, char *argv[]) { return tworld(argc, argv); }