/src/app/app.cpp
https://bitbucket.org/vivkin/gam3b00bs/ · C++ · 347 lines · 258 code · 76 blank · 13 comment · 42 complexity · 8dc94ca391adc8e2040b56379e9be273 MD5 · raw file
- #include "common.h"
- #include "app.h"
- #include "network/network.h"
- #include "sound/sound.h"
- #include "renderer/device.h"
- #include "log.h"
- #include <stdio.h>
- #include <windows.h>
-
- #define CONFIG_FILE "./b00bs.ini"
-
- namespace app
- {
- struct state
- {
- timings time;
- int64 ticks_prev;
- int64 ticks_cur;
-
- HWND hwnd;
- };
- static state g_app = {};
-
- namespace input
- {
- static state key_states[INPUT_KEYS_MAX];
- static state button_states[INPUT_BUTTONS_MAX];
- static int16 cursor_rel[2], wheel_rel;
- static RECT old_clip;
-
- bool init()
- {
- ASSERT(g_app.hwnd);
-
- RAWINPUTDEVICE rid[2]; // keyboard & mouse
- RECT rect;
-
- memset(key_states, 0, sizeof(state) * INPUT_KEYS_MAX);
- memset(button_states, 0, sizeof(state) * INPUT_BUTTONS_MAX);
- cursor_rel[0] = cursor_rel[1] = wheel_rel = 0;
-
- GetWindowRect(g_app.hwnd, &rect);
- SetCursorPos((rect.left + rect.right) / 2, (rect.bottom + rect.top) / 2);
-
- GetClipCursor(&old_clip);
- ClipCursor(&rect);
- //ShowCursor(FALSE); // orly?
-
- rid[0].usUsagePage = 0x01;
- rid[0].usUsage = 0x02; // mouse
- rid[0].dwFlags = 0; //RIDEV_NOLEGACY;
- rid[0].hwndTarget = g_app.hwnd;
-
- rid[1].usUsagePage = 0x01;
- rid[1].usUsage = 0x06; // keyboard
- rid[1].dwFlags = 0; //RIDEV_NOLEGACY;
- rid[1].hwndTarget = g_app.hwnd;
-
- return RegisterRawInputDevices(rid, 2, sizeof(RAWINPUTDEVICE)) != 0;
- }
-
- void term()
- {
- ClipCursor(&old_clip);
- }
-
- bool tick()
- {
- uint32_t i;
-
- cursor_rel[0] = cursor_rel[1] = wheel_rel = 0;
-
- for (i = 0; i < INPUT_KEYS_MAX; ++i)
- if (key_states[i] == DOWN)
- key_states[i] = PRESSED;
-
- for (i = 0; i < INPUT_BUTTONS_MAX; ++i)
- if (button_states[i] == DOWN)
- button_states[i] = PRESSED;
-
- return true;
- }
-
- state get_key_state(uint16 key)
- {
- ASSERT(key < INPUT_KEYS_MAX);
-
- return key_states[key];
- }
-
- state get_button_state(uint16 button)
- {
- ASSERT(button < INPUT_BUTTONS_MAX);
-
- return button_states[button];
- }
-
- int16 get_wheel_rel()
- {
- return wheel_rel;
- }
-
- void get_cursor_pos(int16 *x, int16 *y)
- {
- ASSERT(x && y);
-
- POINT pt;
-
- GetCursorPos(&pt);
- ScreenToClient(g_app.hwnd, &pt);
-
- *x = (int16)pt.x;
- *y = (int16)pt.y;
- }
-
- void get_cursor_rel(int16 *xr, int16 *yr)
- {
- ASSERT(xr && yr);
-
- *xr = cursor_rel[0];
- *yr = cursor_rel[1];
- }
- }
-
- ///////////////////////////////////////////////////////////////////////
- uint32 get_config_int(const char* param_name, const char* section, uint32 default_val)
- {
- return GetPrivateProfileIntA(section,param_name,default_val,CONFIG_FILE);
- }
-
- ///////////////////////////////////////////////////////////////////////
-
- inline static int64 get_ticks()
- {
- int64 ticks;
- QueryPerformanceCounter( (LARGE_INTEGER*) &ticks );
- return ticks;
- }
-
- inline static int64 get_freq()
- {
- int64 f;
- QueryPerformanceFrequency( (LARGE_INTEGER*) &f );
- return f;
- }
-
- static void time_update()
- {
- g_app.ticks_cur = get_ticks();
- double dt = (g_app.ticks_cur - g_app.ticks_prev) / (double)get_freq();
- g_app.time.dt = (float) dt;
- g_app.time.T += dt;
- g_app.ticks_prev = g_app.ticks_cur;
- }
-
- //////////////////////////////////////////////////////////////////////////
-
- static LRESULT APIENTRY wndproc( HWND, UINT, unsigned, long );
-
- static bool init_window()
- {
- //WNDCLASSA wc = { 0, wndproc, 0, 0, GetModuleHandleA(0), LoadIcon(0,IDI_APPLICATION), LoadCursor(0,IDC_ARROW), (HBRUSH)GetStockObject(NULL_BRUSH), 0, "wndclass.4" };
- WNDCLASSA wc = { 0, wndproc, 0, 0, GetModuleHandleA(0), LoadIcon(0,IDI_APPLICATION), LoadCursor(0,IDC_ARROW), (HBRUSH)GetStockObject(WHITE_BRUSH), 0, "wndclass.4" };
- RegisterClassA( &wc );
- g_app.hwnd = CreateWindowA( "wndclass.4", "Render window", WS_VISIBLE | WS_OVERLAPPEDWINDOW, 100, 100, 800, 600, 0, 0, GetModuleHandleA(0), 0 );
- return g_app.hwnd != 0;
- }
-
-
- //////////////////////////////////////////////////////////////////////////
-
-
- // TODO: add more subsystem init here, in the required order
- bool init()
- {
- g_app.ticks_cur = g_app.ticks_prev = get_ticks();
-
- init_window();
- if( !g_app.hwnd )
- {
- fprintf( stderr, "Error: could not create window\n" );
- return false;
- }
- if (!input::init())
- {
- log_write("Error: could not initialize input layer\n");
- return false;
- }
-
- size_t network_pool_sz = get_config_int("network_pool","engine",8 * 1024 * 1024);
- if (!network::init(malloc(network_pool_sz), network_pool_sz))
- {
- fprintf( stderr, "Error: could not initialize network layer\n" );
- return false;
- }
- size_t sound_pool_sz = get_config_int("sound_pool","engine",256 * 1024 * 1024);
- size_t sound_decoder_sz = get_config_int("decoder_pool","engine",8 * 1024 * 1024);
- if (!snd::init(malloc(sound_pool_sz),sound_pool_sz,sound_decoder_sz))
- {
- fprintf( stderr, "Error: could not sound system\n" );
- return false;
- }
-
- RECT rect;
- GetClientRect(g_app.hwnd, &rect);
- renderer_desc_t renderer_desc;
- renderer_desc.depth = D3DFMT_D24X8;
- renderer_desc.width = rect.left - rect.right;
- renderer_desc.height = rect.bottom - rect.top;
- renderer_desc.window = g_app.hwnd;
- renderer_desc.windowed = true;
- renderer_desc.vsync = false;
- renderer_desc.refresh_rate = 0;
- renderer_desc.multisampling = 8;
- if( renderer::init(renderer_desc) )
- {
- log_write("Error: could not initialize gfx layer");
- return false;
- }
-
- if( !test_init() )
- {
- log_write("Error: test data initialization failed");
- return false;
- }
- // moar!
-
-
- return true;
- }
-
- void term()
- {
- renderer::term();
- network::term();
- input::term();
-
- DestroyWindow(g_app.hwnd);
- UnregisterClass("wndclass.4", GetModuleHandleA(0));
- g_app.hwnd = NULL;
- }
-
- // moves the frame
- bool tick()
- {
- MSG msg = {};
- while( TRUE == PeekMessageA(&msg,0,0,0,PM_REMOVE ) )
- {
- if( msg.message == WM_QUIT )
- return false;
- TranslateMessage( &msg );
- DispatchMessageA( &msg );
- }
-
- time_update();
-
- // TODO: do stuff
- // renderer::tick();
-
- Sleep(5);
- return true;
- }
-
- // returns timings for the last tick
- timings time()
- {
- return g_app.time;
- }
-
-
- ///////////////////////////////////////////
-
-
- static LRESULT APIENTRY wndproc( HWND hwnd, UINT msg, UINT wp, LONG lp )
- {
- static bool dont_resize_yet = false;
- static bool resizing = false;
- static RECT rcwnd = {0};
- static RAWINPUT ri;
- static UINT risize;
-
- switch( msg )
- {
- case WM_CREATE:
- GetClientRect( hwnd, &rcwnd );
- break;
-
- case WM_DESTROY: PostQuitMessage(0); return 0;
-
- case WM_ENTERSIZEMOVE: break;
- case WM_SIZE: break;
- case WM_EXITSIZEMOVE: break;
-
- case WM_INPUT:
- {
- risize = sizeof(ri);
-
- if (GetRawInputData((HRAWINPUT)lp, RID_INPUT, (PBYTE)&ri, &risize, sizeof(RAWINPUTHEADER)) == (UINT)(-1))
- return FALSE;
-
- if (ri.header.dwType == RIM_TYPEKEYBOARD && ri.data.keyboard.VKey < INPUT_KEYS_MAX)
- {
- input::state state = input::key_states[ri.data.keyboard.VKey];
-
- if (ri.data.keyboard.Flags & RI_KEY_BREAK)
- state = input::UP;
- else if (state == input::UP)
- state = input::DOWN;
-
- input::key_states[ri.data.keyboard.VKey] = state;
- } else if (ri.header.dwType == RIM_TYPEMOUSE)
- {
- for (uint32 i = 0, d, u; i < INPUT_BUTTONS_MAX; ++i)
- {
- input::state state = input::button_states[i];
-
- d = 1 << (i << 1);
- u = d << 1;
-
- if ((ri.data.mouse.usButtonFlags & d) && state == input::UP)
- state = input::DOWN;
- else if (ri.data.mouse.usButtonFlags & u)
- state = input::UP;
-
- input::button_states[i] = state;
- }
-
- if (ri.data.mouse.usButtonFlags & RI_MOUSE_WHEEL)
- input::wheel_rel += (int16)ri.data.mouse.usButtonData;
-
- if (ri.data.mouse.usFlags == MOUSE_MOVE_RELATIVE)
- {
- input::cursor_rel[0] += (int16)ri.data.mouse.lLastX;
- input::cursor_rel[1] += (int16)ri.data.mouse.lLastY;
- }
- }
-
- return FALSE;
- }
- }
- return DefWindowProcA( hwnd, msg, wp, lp );
- }
-
- }