From 8404143afe48c04e0c84a9bbde08ebf400b7f7fb Mon Sep 17 00:00:00 2001 From: Magnus Auvinen Date: Mon, 20 Oct 2008 17:47:42 +0000 Subject: added SDL support --- src/engine/client/ec_gfx.c | 144 ++++++++++++++++++++++++++++++++++++++++++++- src/engine/client/ec_inp.c | 120 +++++++++++++++++++++++++++++++++++-- src/engine/client/ec_snd.c | 52 +++++++++++++++- 3 files changed, 306 insertions(+), 10 deletions(-) (limited to 'src/engine/client') diff --git a/src/engine/client/ec_gfx.c b/src/engine/client/ec_gfx.c index 9a1e70bf..6a760be7 100644 --- a/src/engine/client/ec_gfx.c +++ b/src/engine/client/ec_gfx.c @@ -1,5 +1,13 @@ /* copyright (c) 2007 magnus auvinen, see licence.txt for more info */ -#include + +#ifdef CONFIG_NO_SDL + #include +#else + #include + #include + #include +#endif + #include #include @@ -77,6 +85,11 @@ static TEXTURE textures[MAX_TEXTURES]; static int first_free_texture; static int memory_usage = 0; +#ifdef CONFIG_NO_SDL +#else + SDL_Surface *screen_surface; +#endif + #if 0 typedef struct { unsigned char r, g, b, a; } PIXEL; @@ -289,12 +302,14 @@ static void draw_quad() flush(); } +#ifdef CONFIG_NO_SDL static void screen_resize(int width, int height) { screen_width = width; screen_height = height; glViewport(0, 0, screen_width, screen_height); } +#endif int gfx_init() { @@ -303,7 +318,6 @@ int gfx_init() screen_width = config.gfx_screen_width; screen_height = config.gfx_screen_height; - glfwInit(); if(config.dbg_stress) { @@ -311,6 +325,10 @@ int gfx_init() screen_height = 240; } + +#ifdef CONFIG_NO_SDL + glfwInit(); + /* set antialiasing */ if(config.gfx_fsaa_samples) glfwOpenWindowHint(GLFW_FSAA_SAMPLES, config.gfx_fsaa_samples); @@ -357,6 +375,62 @@ int gfx_init() /* We don't want to see the window when we run the stress testing */ if(config.dbg_stress) glfwIconifyWindow(); +#else + if(SDL_Init(SDL_INIT_AUDIO|SDL_INIT_VIDEO) < 0) + { + dbg_msg("gfx", "Unable to init SDL: %s\n", SDL_GetError()); + return -1; + } + + atexit(SDL_Quit); + + { + const SDL_VideoInfo *info; + int flags = SDL_OPENGL; + + info = SDL_GetVideoInfo(); + + /* set flags */ + flags = SDL_OPENGL; + flags |= SDL_GL_DOUBLEBUFFER; + flags |= SDL_HWPALETTE; + flags |= SDL_RESIZABLE; + + if(info->hw_available) + flags |= SDL_HWSURFACE; + else + flags |= SDL_SWSURFACE; + + if(info->blit_hw) + flags |= SDL_HWACCEL; + + if(config.gfx_fullscreen) + flags |= SDL_FULLSCREEN; + + /* set gl attributes */ + if(config.gfx_fsaa_samples) + { + SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 1); + SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, config.gfx_fsaa_samples); + } + + SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); + + /* set caption */ + SDL_WM_SetCaption("Teeworlds", "Teeworlds"); + + /* create window */ + screen_surface = SDL_SetVideoMode(screen_width, screen_height, 0, flags); + if(screen_surface == NULL) + { + dbg_msg("gfx", "Unable to set video mode: %s\n", SDL_GetError()); + return -1; + } + } + + SDL_ShowCursor(0); + +#endif /* Init vertices */ if (vertices) @@ -447,12 +521,20 @@ void gfx_mask_op(int mask, int write) int gfx_window_active() { +#ifdef CONFIG_NO_SDL return glfwGetWindowParam(GLFW_ACTIVE) == GL_TRUE ? 1 : 0; +#else + return 1; /* TODO: SDL*/ +#endif } int gfx_window_open() { +#ifdef CONFIG_NO_SDL return glfwGetWindowParam(GLFW_OPENED) == GL_TRUE ? 1 : 0; +#else + return 1; /* TODO: SDL*/ +#endif } VIDEO_MODE fakemodes[] = { @@ -490,12 +572,52 @@ int gfx_get_video_modes(VIDEO_MODE *list, int maxcount) return count; } +#ifdef CONFIG_NO_SDL return glfwGetVideoModes((GLFWvidmode *)list, maxcount); +#else + { + /* TODO: fix this code on osx or windows */ + int num_modes = 0; + SDL_Rect **modes; + + modes = SDL_ListModes(NULL, SDL_OPENGL|SDL_GL_DOUBLEBUFFER|SDL_FULLSCREEN); + if(modes == NULL) + { + /* no modes */ + } + else if(modes == (SDL_Rect**)-1) + { + /* all modes */ + } + else + { + int i; + for(i = 0; modes[i]; ++i) + { + if(num_modes == maxcount) + break; + list[num_modes].width = modes[i]->w; + list[num_modes].height = modes[i]->h; + list[num_modes].red = 8; + list[num_modes].green = 8; + list[num_modes].blue = 8; + num_modes++; + } + } + } + + + return 1; /* TODO: SDL*/ +#endif } void gfx_set_vsync(int val) { +#ifdef CONFIG_NO_SDL glfwSwapInterval(val); +#else + /* TODO: SDL*/ +#endif } int gfx_unload_texture(int index) @@ -697,8 +819,13 @@ void gfx_shutdown() { if (vertices) mem_free(vertices); +#ifdef CONFIG_NO_SDL glfwCloseWindow(); glfwTerminate(); +#else + /* TODO: SDL, is this correct? */ + SDL_Quit(); +#endif } void gfx_screenshot() @@ -783,19 +910,26 @@ void gfx_swap() { static PERFORMACE_INFO pscope = {"glfwSwapBuffers", 0}; perf_start(&pscope); +#ifdef CONFIG_NO_SDL glfwSwapBuffers(); +#else + SDL_GL_SwapBuffers(); +#endif perf_end(); } if(render_enable && config.gfx_finish) glFinish(); +#ifdef CONFIG_NO_SDL { static PERFORMACE_INFO pscope = {"glfwPollEvents", 0}; perf_start(&pscope); glfwPollEvents(); perf_end(); } +#else +#endif } void gfx_screenshot_direct(const char *filename) @@ -1350,10 +1484,16 @@ void gfx_clip_disable() void gfx_minimize() { +#ifdef CONFIG_NO_SDL glfwIconifyWindow(); +#else +#endif } void gfx_maximize() { +#ifdef CONFIG_NO_SDL glfwRestoreWindow(); +#else +#endif } diff --git a/src/engine/client/ec_inp.c b/src/engine/client/ec_inp.c index 00468434..f033ee70 100644 --- a/src/engine/client/ec_inp.c +++ b/src/engine/client/ec_inp.c @@ -1,14 +1,25 @@ /* copyright (c) 2007 magnus auvinen, see licence.txt for more info */ #include -#include +#ifdef CONFIG_NO_SDL + #include +#else + #include +#endif #include #include #include -static int keyboard_state[2][1024]; /* TODO: fix this!! */ +#ifdef CONFIG_NO_SDL +static int keyboard_state[2][1024] = {{0}}; /* TODO: fix this!! */ +#else +static unsigned char keyboard_state[2][1024] = {{0}}; /* TODO: fix this!! */ +#endif static int keyboard_current = 0; + +#ifdef CONFIG_NO_SDL static int keyboard_first = 1; +#endif static struct @@ -20,14 +31,16 @@ static struct static unsigned char input_state[2][1024] = {{0}, {0}}; static int input_current = 0; +#ifdef CONFIG_NO_SDL static unsigned int last_release = 0; +#endif static unsigned int release_delta = -1; void inp_mouse_relative(int *x, int *y) { static int last_x = 0, last_y = 0; static int last_sens = 100.0f; - int nx, ny; + int nx = 0, ny = 0; float sens = config.inp_mousesens/100.0f; if(last_sens != config.inp_mousesens) @@ -37,8 +50,9 @@ void inp_mouse_relative(int *x, int *y) last_sens = config.inp_mousesens; } - +#ifdef CONFIG_NO_SDL glfwGetMousePos(&nx, &ny); + nx *= sens; ny *= sens; @@ -46,6 +60,12 @@ void inp_mouse_relative(int *x, int *y) *y = ny-last_y; last_x = nx; last_y = ny; +#else + SDL_GetRelativeMouseState(&nx, &ny); + + *x = nx*sens; + *y = ny*sens; +#endif } enum @@ -88,6 +108,7 @@ INPUT_EVENT inp_get_event(int index) return input_events[index]; } +#ifdef CONFIG_NO_SDL static void char_callback(int character, int action) { if(action == GLFW_PRESS && character < 256) @@ -177,6 +198,25 @@ void inp_mouse_mode_relative() /*if (!config.gfx_debug_resizable)*/ glfwDisable(GLFW_MOUSE_CURSOR); } +#else + +void inp_init() +{ + SDL_EnableUNICODE(1); +} + +void inp_mouse_mode_absolute() +{ + SDL_ShowCursor(1); + SDL_WM_GrabInput(SDL_GRAB_OFF); +} + +void inp_mouse_mode_relative() +{ + SDL_ShowCursor(0); + SDL_WM_GrabInput(SDL_GRAB_ON); +} +#endif int inp_mouse_doubleclick() { @@ -211,8 +251,9 @@ int inp_button_pressed(int button) { return keyboard_state[keyboard_current][but void inp_update() { +#ifdef CONFIG_NO_SDL int i, v; - + /* clear and begin count on the other one */ mem_zero(&input_count[input_current], sizeof(input_count[input_current])); mem_copy(input_state[input_current], input_state[input_current^1], sizeof(input_state[input_current])); @@ -234,6 +275,75 @@ void inp_update() v = glfwGetKey(i) == GLFW_PRESS ? 1 : 0; keyboard_state[keyboard_current][i] = v; } +#else + int i; + + /* clear and begin count on the other one */ + mem_zero(&input_count[input_current], sizeof(input_count[input_current])); + mem_copy(input_state[input_current], input_state[input_current^1], sizeof(input_state[input_current])); + input_current^=1; + + { + Uint8 *state = SDL_GetKeyState(&i); + if(i >= KEY_LAST) + i = KEY_LAST-1; + mem_copy(keyboard_state[keyboard_current], state, i); + } + + keyboard_state[keyboard_current][KEY_MOUSE_1] = 0; + keyboard_state[keyboard_current][KEY_MOUSE_2] = 0; + keyboard_state[keyboard_current][KEY_MOUSE_3] = 0; + i = SDL_GetMouseState(NULL, NULL); + if(i&SDL_BUTTON(1)) keyboard_state[keyboard_current][KEY_MOUSE_1] = 1; + if(i&SDL_BUTTON(2)) keyboard_state[keyboard_current][KEY_MOUSE_2] = 1; + if(i&SDL_BUTTON(3)) keyboard_state[keyboard_current][KEY_MOUSE_3] = 1; + + { + SDL_Event event; + + while(SDL_PollEvent(&event)) + { + int key = -1; + int action = INPFLAG_PRESS; + switch (event.type) + { + /* handle keys */ + case SDL_KEYDOWN: + if(event.key.keysym.unicode < 255) + add_event(event.key.keysym.unicode, 0, 0); + key = event.key.keysym.sym; + break; + case SDL_KEYUP: + action = INPFLAG_RELEASE; + key = event.key.keysym.sym; + break; + + /* handle mouse buttons */ + case SDL_MOUSEBUTTONUP: + action = INPFLAG_RELEASE; + case SDL_MOUSEBUTTONDOWN: + key = KEY_MOUSE_1+event.button.button-1; + break; + + /* other messages */ + case SDL_QUIT: + exit(0); + } + + /* */ + if(key != -1) + { + input_count[input_current^1][key].presses++; + input_state[input_current^1][key] = 1; + add_event(0, key, action); + } + + } + } +#endif + + if(inp_key_pressed('q')) + exit(1); /* handle mouse wheel */ /* diff --git a/src/engine/client/ec_snd.c b/src/engine/client/ec_snd.c index 9dcbafaa..c3563863 100644 --- a/src/engine/client/ec_snd.c +++ b/src/engine/client/ec_snd.c @@ -3,8 +3,14 @@ #include #include +#ifdef CONFIG_NO_SDL + #include +#else + #include +#endif + + #include -#include #include #include #include @@ -232,16 +238,24 @@ static void mix(short *final_out, unsigned frames) } } +#ifdef CONFIG_NO_SDL +static PaStream *stream; static int pacallback(const void *in, void *out, unsigned long frames, const PaStreamCallbackTimeInfo* time, PaStreamCallbackFlags status, void *user) { mix(out, frames); return 0; } +#else +static void sdlcallback(void *unused, Uint8 *stream, int len) +{ + mix((short *)stream, len/2/2); +} +#endif -static PaStream *stream; int snd_init() { +#ifdef CONFIG_NO_SDL PaStreamParameters params; PaError err = Pa_Initialize(); @@ -293,7 +307,36 @@ int snd_init() pacallback, /* specify our custom callback */ 0x0); /* pass our data through to callback */ err = Pa_StartStream(stream); +#else + SDL_AudioSpec format; + + sound_lock = lock_create(); + + if(!config.snd_enable) + return 0; + + mixing_rate = config.snd_rate; + + /* Set 16-bit stereo audio at 22Khz */ + format.freq = config.snd_rate; + format.format = AUDIO_S16; + format.channels = 2; + format.samples = 512; /* A good value for games */ + format.callback = sdlcallback; + format.userdata = NULL; + + /* Open the audio device and start playing sound! */ + if(SDL_OpenAudio(&format, NULL) < 0) + { + dbg_msg("client/sound", "unable to open audio: %s", SDL_GetError()); + return -1; + } + else + dbg_msg("client/sound", "sound init successful"); + SDL_PauseAudio(0); + +#endif sound_enabled = 1; snd_update(); /* update the volume */ return 0; @@ -319,9 +362,12 @@ int snd_update() int snd_shutdown() { +#ifdef CONFIG_NO_SDL Pa_StopStream(stream); Pa_Terminate(); - +#else + SDL_CloseAudio(); +#endif lock_destroy(sound_lock); return 0; -- cgit 1.4.1