From 72ee43e9470c6fdbc6ed7ff92b85dfa5062c5762 Mon Sep 17 00:00:00 2001 From: Stijn Buys Date: Tue, 6 Jan 2015 18:51:37 +0000 Subject: Added separate event handlers for mouse button clicks and mouse wheel movement. --- src/client/video.cc | 229 ++++++++++++++++++++++------------------------------ 1 file changed, 98 insertions(+), 131 deletions(-) (limited to 'src/client/video.cc') diff --git a/src/client/video.cc b/src/client/video.cc index 0f0c984..f3ba136 100644 --- a/src/client/video.cc +++ b/src/client/video.cc @@ -17,7 +17,7 @@ #include "ui/ui.h" #include "ui/paint.h" -#include +#include using namespace render; @@ -41,32 +41,49 @@ core::Cvar *draw_clock = 0; namespace video { -float fullscreen = 0; - -int bpp = 0; -int flags = 0; +bool fullscreen = 0; int width = 0; int height = 0; -int width_prev = 0; -int height_prev = 0; - // default resolution and window size const int width_default = 1024; const int height_default = 768; +SDL_Window* sdlwindow = 0; +Uint32 sdlwindow_flags = 0; + std::string loader_message; bool is_loading = false; -void set_caption() +void draw_loader() { - // set window caption - std::string window_title(core::name()); - window_title += ' '; - window_title.append(core::version()); + render::Camera::ortho(); + + gl::enable(GL_BLEND); + + gl::color(1.0f, 1.0f, 1.0f, 1.0f); + + + const std::string loader_texture("bitmaps/loader"); + const math::Vector2f size(render::State::width(), render::State::height()); + const math::Vector2f pos; + ui::Paint::draw_bitmap(pos, size, loader_texture, true); + + if (loader_message.size()) { + using render::Text; + gl::enable(GL_TEXTURE_2D); + Text::setfont("gui", 12, 18); + Text::setcolor('N'); //set normal color + Text::draw(Text::fontwidth(), Text::fontheight(), loader_message); + gl::disable(GL_TEXTURE_2D); + } + + gl::disable(GL_BLEND); + + swap_buffers(); - SDL_WM_SetCaption(window_title.c_str(), core::name().c_str()); + is_loading = true; } bool init() @@ -110,12 +127,19 @@ bool init() } - // set the window icon + // window caption + std::string sdlwindow_title(core::name()); + sdlwindow_title += ' '; + sdlwindow_title.append(core::version()); + + // window icon /* * FIXME * store the icon as binary data * and use SDL_CreateRGBSurfaceFrom to create the icon */ + /* + * filesystem::File *iconfile = filesystem::open("bitmaps/icon.bmp"); if (iconfile) { std::string iconfilename = iconfile->path(); @@ -130,109 +154,69 @@ bool init() SDL_WM_SetIcon(image, NULL); } } - - const SDL_VideoInfo* sdl_videoinfo = SDL_GetVideoInfo(); - if (!sdl_videoinfo) { - con_error << "SDL_GetVideoInfo() failed: " << SDL_GetError() << std::endl; - return false; - } - - bpp = sdl_videoinfo->vfmt->BitsPerPixel; - if (bpp == 32) { - SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8); - SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8); - SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8); - SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 8); - } else if (bpp == 24) { - SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 6); - SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 6); - SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 6); - SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 6); - } else if (bpp == 16) { - SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 4); - SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 4); - SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 4); - SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 4); - } else { - con_warn << "Display depth " << bpp << " is not supported!" << std::endl; - } - - SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); - + */ - width_prev = width; - height_prev = height; - - width = (int) r_width->value(); - height = (int) r_height->value(); + SDL_InitSubSystem(SDL_INIT_VIDEO); - fullscreen = r_fullscreen->value(); + fullscreen = (r_fullscreen->value() > 0.0f); - if (r_fullscreen->value()) { - flags = SDL_OPENGL | SDL_FULLSCREEN; - } else { - if (r_windowwidth->value() && r_windowheight->value()) { + if (fullscreen) + { + sdlwindow_flags = SDL_WINDOW_OPENGL | SDL_WINDOW_FULLSCREEN_DESKTOP | SDL_WINDOW_BORDERLESS; + sdlwindow = SDL_CreateWindow(sdlwindow_title.c_str(), SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 0, 0, sdlwindow_flags); + } + else + { + if (r_windowwidth->value() && r_windowheight->value()) + { width = (int) r_windowwidth->value(); height = (int) r_windowheight->value(); + } + else + { + width = width_default; + height = height_default; } - flags = SDL_OPENGL; + sdlwindow_flags = SDL_WINDOW_OPENGL; #ifndef _WIN32 - flags |= SDL_RESIZABLE; + sdlwindow_flags |= SDL_WINDOW_RESIZABLE; #endif + sdlwindow = SDL_CreateWindow(sdlwindow_title.c_str(), SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, width, height, sdlwindow_flags); } - - if (!SDL_SetVideoMode(width, height, bpp, flags)) { - con_warn << "Failed to set video mode " << width << "x" << height << "x" << bpp << "bpp" << std::endl; - if (width_prev && height_prev) { - width = width_prev; - height = height_prev; - if (!SDL_SetVideoMode(width, height, bpp, flags)) { - con_error << "Failed to restore video mode " << width << "x" << height << "x" << bpp << "bpp" << std::endl; - return false; - } - } else - return false; + + if (!sdlwindow) + { + con_error << "Failed to initialize SDL window: " << SDL_GetError() << std::endl; + return false; + } + + SDL_GLContext glcontext = SDL_GL_CreateContext(sdlwindow); + if (!glcontext) + { + con_error << "Failed to initialize OpenGL context: " << SDL_GetError() << std::endl; + return false; } - con_print << " video mode " << width << "x" << height << "x" << bpp << "bpp " << (fullscreen ? "fullscreen " : "window") << std::endl; - -#ifdef HAVE_DEBUG_MESSAGES - - int red, green, blue, alpha, depth; - - SDL_GL_GetAttribute(SDL_GL_RED_SIZE, &red); - SDL_GL_GetAttribute(SDL_GL_GREEN_SIZE, &green); - SDL_GL_GetAttribute(SDL_GL_RED_SIZE, &blue); - SDL_GL_GetAttribute(SDL_GL_ALPHA_SIZE, &alpha); - SDL_GL_GetAttribute(SDL_GL_DEPTH_SIZE, &depth); - - con_debug << " visual r: " << red << " g: " << green << " blue: " << blue << " alpha: " << alpha << " depth: " << depth << std::endl; - -#endif // HAVE_DEBUG_MESSAGES - - // set window caption - set_caption(); + SDL_GetWindowSize(sdlwindow, &width, &height); + // save window width and height if (fullscreen) { (*r_width) = width; (*r_height) = height; } else { (*r_windowwidth) = width; - (*r_windowheight) = height; - + (*r_windowwidth) = height; } // resize user interface ui::root()->set_size((float) width, (float) height); ui::root()->event_resize(); - // to grab or not to grab + // grab input core::Cvar *input_grab = core::Cvar::find("input_grab"); - if (ui::console()->visible() || (input_grab && input_grab->value())) { - SDL_WM_GrabInput(SDL_GRAB_OFF); - SDL_ShowCursor(SDL_ENABLE); + if (!ui::console()->visible() || (input_grab && input_grab->value())) { + SDL_SetRelativeMouseMode(SDL_TRUE); } else { - SDL_WM_GrabInput(SDL_GRAB_ON); - SDL_ShowCursor(SDL_DISABLE); + SDL_SetRelativeMouseMode(SDL_FALSE); } // initialize renderer @@ -243,6 +227,9 @@ bool init() // initialize target drawer targets::init(); + + // draw loader screen + draw_loader(); return true; } @@ -255,13 +242,12 @@ void resize(int w, int h) if (w < 320) w = 320; if (h < 200) h = 200; - if (SDL_SetVideoMode(w, h, bpp, flags)) { - render::resize(w, h); - ui::root()->set_size(w, h); - ui::root()->event_resize(); - } else { - con_warn << "Could not resize window!" << std::endl; - } + render::resize(w, h); + ui::root()->set_size(w, h); + ui::root()->event_resize(); + + (*r_windowwidth) = w; + (*r_windowwidth) = h; } void restart() @@ -305,32 +291,9 @@ void set_loader_message(const char *message) frame_loader(); } -void draw_loader() +void swap_buffers() { - render::Camera::ortho(); - - gl::enable(GL_BLEND); - - gl::color(1.0f, 1.0f, 1.0f, 1.0f); - - - const std::string loader_texture("bitmaps/loader"); - const math::Vector2f size(render::State::width(), render::State::height()); - const math::Vector2f pos; - ui::Paint::draw_bitmap(pos, size, loader_texture, true); - - if (loader_message.size()) { - using render::Text; - gl::enable(GL_TEXTURE_2D); - Text::setfont("gui", 12, 18); - Text::setcolor('N'); //set normal color - Text::draw(Text::fontwidth(), Text::fontheight(), loader_message); - gl::disable(GL_TEXTURE_2D); - } - - gl::disable(GL_BLEND); - - is_loading = true; + SDL_GL_SwapWindow(sdlwindow); } void frame_loader() @@ -340,13 +303,13 @@ void frame_loader() draw_loader(); - SDL_GL_SwapBuffers(); + swap_buffers(); } void frame(float elapsed) { // detect fullscreen/windowed mode switch - if (fullscreen != r_fullscreen->value()) + if (fullscreen != (r_fullscreen->value() > 0.0f)) restart(); using namespace render; @@ -401,7 +364,7 @@ void frame(float elapsed) gl::disable(GL_TEXTURE_2D); gl::disable(GL_BLEND); - SDL_GL_SwapBuffers(); + swap_buffers(); } void shutdown() @@ -411,11 +374,15 @@ void shutdown() targets::shutdown(); render::shutdown(); + + SDL_SetRelativeMouseMode(SDL_FALSE); + + SDL_DestroyWindow(sdlwindow); + SDL_QuitSubSystem(SDL_INIT_VIDEO); + width = 0; height = 0; - - SDL_QuitSubSystem(SDL_INIT_VIDEO); } } // namespace video -- cgit v1.2.3