#include #include #include #include "sdl.h" #include "ogl.h" #include "detect.h" #include "timer.h" #include "input.h" #include "lib.h" #include "posix.h" #include "res/res.h" #ifdef _M_IX86 #include "sysdep/ia32.h" #endif #include "ps/Config.h" #include "MapReader.h" #include "Terrain.h" #include "Renderer.h" #include "Model.h" #include "UnitManager.h" #include "BaseEntityCollection.h" #include "Entity.h" #include "EntityHandles.h" #include "EntityManager.h" #ifndef NO_GUI #include "gui/GUI.h" #endif u32 game_ticks; bool keys[SDLK_LAST]; #include // flag to disable extended GL extensions until fix found - specifically, crashes // using VBOs on laptop Radeon cards static bool g_NoGLVBO=false; static bool g_VSync = false; static float g_Gamma = 1.0f; // mapfile to load or null for no map (and to use default terrain) static const char* g_MapFile=0; static Handle font; extern CCamera g_Camera; extern void terr_init(); extern void terr_update(float time); extern bool terr_handler(const SDL_Event& ev); extern int allow_reload(); extern int dir_add_watch(const char* const dir, bool watch_subdirs); static int write_sys_info() { get_gfx_info(); struct utsname un; uname(&un); FILE* const f = fopen("../system/system_info.txt", "w"); if(!f) return -1; // .. OS fprintf(f, "%s %s (%s)\n", un.sysname, un.release, un.version); // .. CPU fprintf(f, "%s, %s", un.machine, cpu_type); if(cpus > 1) fprintf(f, " (x%d)", cpus); if(cpu_freq != 0.0f) { if(cpu_freq < 1e9) fprintf(f, ", %.2f MHz\n", cpu_freq*1e-6); else fprintf(f, ", %.2f GHz\n", cpu_freq*1e-9); } else fprintf(f, "\n"); // .. memory fprintf(f, "%lu MB RAM; %lu MB free\n", tot_mem/MB, avl_mem/MB); // .. graphics card fprintf(f, "%s\n", gfx_card); fprintf(f, "%s\n", gfx_drv); // .. network name / ips char hostname[100]; // possibly nodename != hostname gethostname(hostname, sizeof(hostname)); fprintf(f, "%s\n", hostname); hostent* h = gethostbyname(hostname); if(h) { struct in_addr** ips = (struct in_addr**)h->h_addr_list; for(int i = 0; ips && ips[i]; i++) fprintf(f, "%s ", inet_ntoa(*ips[i])); fprintf(f, "\n"); } fclose(f); return 0; } // error before GUI is initialized: display message, and quit // TODO: localization static void display_startup_error(const wchar_t* msg) { const wchar_t* caption = L"0ad startup problem"; write_sys_info(); wdisplay_msg(caption, msg); exit(1); } // error before GUI is initialized: display message, and quit // TODO: localization static void display_startup_error(const char* msg) { const char* caption = "0ad startup problem"; write_sys_info(); display_msg(caption, msg); exit(1); } static int set_vmode(int w, int h, int bpp) { SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24); SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); if(!SDL_SetVideoMode(w, h, bpp, SDL_OPENGL|SDL_FULLSCREEN)) return -1; glViewport(0, 0, w, h); #ifndef NO_GUI g_GUI.UpdateResolution(); #endif oglInit(); // required after each mode change return 0; } // break out of main loop static bool quit = false; static bool handler(const SDL_Event& ev) { int c; switch(ev.type) { case SDL_KEYDOWN: c = ev.key.keysym.sym; keys[c] = true; switch(c) { case SDLK_ESCAPE: quit = true; break; } break; case SDL_KEYUP: c = ev.key.keysym.sym; keys[c] = false; break; } return 0; } ////////////////////////////////////////////////////////////////////////////////////////////////// // RenderTerrain: iterate through all terrain patches and submit all patches in viewing frustum to // the renderer void RenderTerrain() { CFrustum frustum=g_Camera.GetFustum(); u32 patchesPerSide=g_Terrain.GetPatchesPerSide(); for (uint j=0; jGetBounds())) { g_Renderer.Submit(patch); } } } } ////////////////////////////////////////////////////////////////////////////////////////////////// // RenderModels: iterate through model list and submit all models in viewing frustum to the // Renderer void RenderModels() { CFrustum frustum=g_Camera.GetFustum(); const std::vector& units=g_UnitMan.GetUnits(); uint i; for (i=0;im_Model->GetBounds())) { g_Renderer.Submit(units[i]->m_Model); } } } ///////////////////////////////////////////////////////////////////////////////////////////// // RenderNoCull: render absolutely everything to a blank frame to force renderer // to load required assets void RenderNoCull() { g_Renderer.BeginFrame(); g_Renderer.SetCamera(g_Camera); uint i,j; const std::vector& units=g_UnitMan.GetUnits(); for (i=0;im_Model); } u32 patchesPerSide=g_Terrain.GetPatchesPerSide(); for (j=0; j& units=g_UnitMan.GetUnits(); for (uint i=0;im_Model->Update(time); } g_EntityManager.updateAll( time ); } void ParseArgs(int argc, char* argv[]) { for (int i=1;i TICK_TIME) { game_ticks++; in_get_events(); do_tick(); time0 += TICK_TIME; } UpdateWorld(float(time1-time0)); terr_update(float(time1-time0)); Render(); SDL_GL_SwapBuffers(); calc_fps(); #else double time1 = get_time(); in_get_events(); UpdateWorld(float(time1-time0)); terr_update(float(time1-time0)); Render(); SDL_GL_SwapBuffers(); calc_fps(); time0=time1; #endif } #ifndef NO_GUI g_GUI.Destroy(); delete CGUI::GetSingletonPtr(); // again, we should have all singleton deletes somewhere #endif delete &g_Config; delete &g_EntityManager; delete &g_EntityTemplateCollection; return 0; }