/* * Read the MCLX-enhanced page header cache. Verify the first one, which * is a pointer to the page header for address 1MB, and take the rest at * blind faith. Note that the page headers do not include the 64K dump * header offset, which must be added to the values found. */ static void mclx_cache_page_headers_v8(void) { int i; uint64_t physaddr1, physaddr2, page_headers[MCLX_PAGE_HEADERS]; dump_page_t dump_page, *dp; ulong granularity; size_t dh_size; if (LKCD_DEBUG(2)) /* dump headers have all been read */ return; if (lkcd->total_pages > MEGABYTES(1))/* greater than 4G not supported */ return; dh_size = sizeof(dump_header_t); if ((((dump_header_t *)lkcd->dump_header)->dh_version & LKCD_DUMP_VERSION_NUMBER_MASK) == LKCD_DUMP_V9) dh_size += sizeof(uint64_t); if (lseek(lkcd->fd, dh_size, SEEK_SET) == -1) return; if (read(lkcd->fd, page_headers, MCLX_V1_PAGE_HEADER_CACHE) != MCLX_V1_PAGE_HEADER_CACHE) return; dp = &dump_page; /* * Determine the granularity between offsets. */ if (lseek(lkcd->fd, page_headers[0] + lkcd_offset_to_first_page, SEEK_SET) == -1) return; if (read(lkcd->fd, dp, lkcd->page_header_size) != lkcd->page_header_size) return; physaddr1 = (dp->dp_address - lkcd->kvbase) << lkcd->page_shift; if (lseek(lkcd->fd, page_headers[1] + lkcd_offset_to_first_page, SEEK_SET) == -1) return; if (read(lkcd->fd, dp, lkcd->page_header_size) != lkcd->page_header_size) return; physaddr2 = (dp->dp_address - lkcd->kvbase) << lkcd->page_shift; if ((physaddr1 % MEGABYTES(1)) || (physaddr2 % MEGABYTES(1)) || (physaddr2 < physaddr1)) return; granularity = physaddr2 - physaddr1; for (i = 0; i < (MCLX_PAGE_HEADERS-1); i++) { if (!page_headers[i]) break; lkcd->curhdroffs = page_headers[i] + lkcd_offset_to_first_page; set_mb_benchmark((granularity * (i+1))/lkcd->page_size); } }
/* * Read the MCLX-enhanced page header cache. Verify the first one, which * is a pointer to the page header for address 1MB, and take the rest at * blind faith. Note that the page headers do not include the 64K dump * header offset, which must be added to the values found. */ static void mclx_cache_page_headers_v3(void) { int i; uint64_t physaddr1, physaddr2, page_headers[MCLX_PAGE_HEADERS]; dump_page_t dump_page, *dp; ulong granularity; if (LKCD_DEBUG(2)) /* dump headers have all been read */ return; if (lkcd->total_pages > MEGABYTES(1))/* greater than 4G not supported */ return; if (lseek(lkcd->fd, sizeof(dump_header_t), SEEK_SET) == -1) return; if (read(lkcd->fd, page_headers, MCLX_V1_PAGE_HEADER_CACHE) != MCLX_V1_PAGE_HEADER_CACHE) return; dp = &dump_page; /* * Determine the granularity between offsets. */ if (lseek(lkcd->fd, page_headers[0] + LKCD_OFFSET_TO_FIRST_PAGE, SEEK_SET) == -1) return; if (read(lkcd->fd, dp, lkcd->page_header_size) != lkcd->page_header_size) return; physaddr1 = (dp->dp_address - lkcd->kvbase) << lkcd->page_shift; if (lseek(lkcd->fd, page_headers[1] + LKCD_OFFSET_TO_FIRST_PAGE, SEEK_SET) == -1) return; if (read(lkcd->fd, dp, lkcd->page_header_size) != lkcd->page_header_size) return; physaddr2 = (dp->dp_address - lkcd->kvbase) << lkcd->page_shift; if ((physaddr1 % MEGABYTES(1)) || (physaddr2 % MEGABYTES(1)) || (physaddr2 < physaddr1)) return; granularity = physaddr2 - physaddr1; for (i = 0; i < (MCLX_PAGE_HEADERS-1); i++) { if (!page_headers[i]) break; lkcd->curhdroffs = page_headers[i] + LKCD_OFFSET_TO_FIRST_PAGE; set_mb_benchmark((granularity * (i+1))/lkcd->page_size); } }
DLLEXPORT void StartUp(gameapi_t *Api, gamestate_t *GameState) { GameState->Memory = Api->AllocateMemory(MEGABYTES(32)); FrameMemory = AllocateSubGameMemory(&GameState->Memory, MEGABYTES(10)); ArtsMemory = AllocateSubGameMemory(&GameState->Memory, MEGABYTES(10)); LoadArts("game_items.art", Api, &ArtsMemory); GameState->Initialized = true; }
long long cvtnum(char *s) { long long i; char *sp; int c; i = strtoll(s, &sp, 0); if (i == 0 && sp == s) return -1LL; if (*sp == '\0') return i; if (sp[1] != '\0') return -1LL; c = tolower(*sp); switch (c) { case 'k': return KILOBYTES(i); case 'm': return MEGABYTES(i); case 'g': return GIGABYTES(i); case 't': return TERABYTES(i); case 'p': return PETABYTES(i); case 'e': return EXABYTES(i); } return -1LL; }
void cvtstr( double value, char *str, size_t size) { const char *fmt; int precise; precise = ((double)value * 1000 == (double)(int)value * 1000); if (value >= EXABYTES(1)) { fmt = precise ? "%.f EiB" : "%.3f EiB"; snprintf(str, size, fmt, TO_EXABYTES(value)); } else if (value >= PETABYTES(1)) { fmt = precise ? "%.f PiB" : "%.3f PiB"; snprintf(str, size, fmt, TO_PETABYTES(value)); } else if (value >= TERABYTES(1)) { fmt = precise ? "%.f TiB" : "%.3f TiB"; snprintf(str, size, fmt, TO_TERABYTES(value)); } else if (value >= GIGABYTES(1)) { fmt = precise ? "%.f GiB" : "%.3f GiB"; snprintf(str, size, fmt, TO_GIGABYTES(value)); } else if (value >= MEGABYTES(1)) { fmt = precise ? "%.f MiB" : "%.3f MiB"; snprintf(str, size, fmt, TO_MEGABYTES(value)); } else if (value >= KILOBYTES(1)) { fmt = precise ? "%.f KiB" : "%.3f KiB"; snprintf(str, size, fmt, TO_KILOBYTES(value)); } else { snprintf(str, size, "%f bytes", value); } }
void showmemsize(void) { Sint pagesize = (Sint) sysconf((Sysconfargtype) _SC_PAGESIZE); Sint physpages = (Sint) sysconf((Sysconfargtype) _SC_PHYS_PAGES); DEBUG1(1,"# pagesize = %ld\n",(Showsint) pagesize); DEBUG1(1,"# number of physical pages = %ld\n",(Showsint) physpages); DEBUG1(1,"# memory size = %.0f MB\n",MEGABYTES(pagesize * physpages)); }
static void mmaddspace(Uint space) { currentspace += space; if(currentspace > spacepeak) { spacepeak = currentspace; DEBUG1(2,"# mmap spacepeak = %.2f reached\n",MEGABYTES(spacepeak)); } }
DLLEXPORT void RunFrame(gameapi_t *Api, gamestate_t *GameState) { // Clear frame memory each time (e.g. it works like the stack) memset(FrameMemory.Data, 0, FrameMemory.Size); /* // NOTE: keep this code in this comment for now in case I forget // how to use this system! u8 Flags = BitmapFlag_Alpha; if (GameState->Input.Right) { Flags |= BitmapFlag_AlignRight; } else if (GameState->Input.Left) { Flags |= BitmapFlag_AlignLeft; } if (GameState->Input.Up) { Flags |= BitmapFlag_AlignTop; } else if (GameState->Input.Down) { Flags |= BitmapFlag_AlignBottom; } renderlist_t RenderList = AllocateRenderList(&FrameMemory, MEGABYTES(1)); Clear(&RenderList); DrawBitmap(&RenderList, GameItem_ruta, 0.8, 0.1f, White(), 0.5f, 1); DrawBitmap(&RenderList, GameItem_demon, 0, 0, Color(1,0,0,0.5f), 1, 1, BitmapFlag_Alpha); DrawBitmap(&RenderList, GameItem_demon, 0, 0, Color(0,0,1,0.5f), 1, 1, BitmapFlag_AlignBottom|BitmapFlag_Alpha); DrawBitmap(&RenderList, GameItem_demon, 0, 0, Color(1,0,1,0.75f), 1.5, 1, BitmapFlag_AlignRight|BitmapFlag_Alpha); DrawBitmap(&RenderList, GameItem_demon, 0.15f, 0.15f, Color(0,1,0,0.25f), 1, 1, Flags); PerformRender(&RenderList, GameState); */ if (GameState->Input.Right) { X += 0.01; } else if (GameState->Input.Left) { X -= 0.01; } if (GameState->Input.Up) { Y -= 0.01; } else if (GameState->Input.Down) { Y += 0.01; } const float CharWidth = X * (8 / (float)GameState->DrawBuffer.Width); float cX = 0; #define DBGPRINT(c,x) DrawBitmap(&RenderList, GameItem_debugfont_##c, cX, 0, White(), X, Y); cX += CharWidth; renderlist_t RenderList = AllocateRenderList(&FrameMemory, MEGABYTES(1)); Clear(&RenderList); DBGPRINT(a,0); DBGPRINT(b,0.1); DBGPRINT(c,0.15); DBGPRINT(d,0.2); DBGPRINT(e,0.25); DBGPRINT(f,0.3); PerformRender(&RenderList, GameState); }
void CL_AllocateDemoPoints(void) { CL_FreeDemoPoints(); maxRewindBackups = cl_maxRewindBackups->integer; if (maxRewindBackups <= 0) { maxRewindBackups = MAX_REWIND_BACKUPS; } rewindBackups = (rewindBackups_t *)Com_Allocate(sizeof(rewindBackups_t) * maxRewindBackups); if (!rewindBackups) { Com_FuncError("couldn't allocate %.2f MB for rewind backups\n", MEGABYTES(sizeof(rewindBackups_t) * maxRewindBackups)); return; } Com_FuncPrinf("allocated %.2f MB for rewind backups\n", MEGABYTES(sizeof(rewindBackups_t) * maxRewindBackups)); Com_Memset(rewindBackups, 0, sizeof(rewindBackups_t) * maxRewindBackups); }
static void addspace(Uint space) { if(currentspace == 0) { setmaxspace(); currentspace = space; } else { currentspace += space; } if(currentspace > spacepeak) { spacepeak = currentspace; DEBUG1(2,"# spacepeak = %.2f reached\n",MEGABYTES(spacepeak)); } }
static int APIENTRY WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow ) { AllocConsole(); //create a console freopen( "conin$","r",stdin ); freopen( "conout$","w",stdout ); freopen( "conout$","w",stderr ); printf( "Program Started, console initialized\n" ); appInfo.appInstance = hInstance; const char WindowName[] = "Wet Clay"; appInfo.running = true; appInfo.isFullScreen = false; appInfo.mSecsPerFrame = 16; //60FPS appInfo.wc.cbSize = sizeof(WNDCLASSEX); appInfo.wc.style = CS_OWNDC; appInfo.wc.lpfnWndProc = WndProc; appInfo.wc.cbClsExtra = 0; appInfo.wc.cbWndExtra = 0; appInfo.wc.hInstance = appInfo.appInstance; appInfo.wc.hIcon = LoadIcon(NULL, IDI_APPLICATION); appInfo.wc.hCursor = LoadCursor(NULL, IDC_ARROW); appInfo.wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1); appInfo.wc.lpszMenuName = NULL; appInfo.wc.lpszClassName = WindowName; appInfo.wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION); if( !RegisterClassEx( &appInfo.wc ) ) { printf( "Failed to register class\n" ); appInfo.running = false; return -1; } // center position of the window appInfo.windowPosX = (GetSystemMetrics(SM_CXSCREEN) / 2) - (SCREEN_WIDTH / 2); appInfo.windowPosY = (GetSystemMetrics(SM_CYSCREEN) / 2) - (SCREEN_HEIGHT / 2); // set up the window for a windowed application by default //long wndStyle = WS_OVERLAPPEDWINDOW; if( appInfo.isFullScreen ) { appInfo.windowPosX = 0; appInfo.windowPosY = 0; //change resolution before the window is created //SysSetDisplayMode(width, height, SCRDEPTH); //TODO: implement } // at this point WM_CREATE message is sent/received // the WM_CREATE branch inside WinProc function will execute here appInfo.hwnd = CreateWindowEx(0, WindowName, "Wet Clay App", WS_BORDER, appInfo.windowPosX, appInfo.windowPosY, SCREEN_WIDTH, SCREEN_HEIGHT, NULL, NULL, appInfo.appInstance, NULL); GetClientRect( appInfo.hwnd, appInfo.windowRect ); PIXELFORMATDESCRIPTOR pfd = { sizeof( PIXELFORMATDESCRIPTOR ), 1, PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER, //Flags PFD_TYPE_RGBA, //The kind of framebuffer. RGBA or palette. 32, //Colordepth of the framebuffer. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 32, //Number of bits for the depthbuffer 0, //Number of bits for the stencilbuffer 0, //Number of Aux buffers in the framebuffer. 0, 0, 0, 0, 0 }; appInfo.deviceContext = GetDC( appInfo.hwnd ); int letWindowsChooseThisPixelFormat; letWindowsChooseThisPixelFormat = ChoosePixelFormat( appInfo.deviceContext, &pfd ); SetPixelFormat( appInfo.deviceContext, letWindowsChooseThisPixelFormat, &pfd ); appInfo.openglRenderContext = wglCreateContext( appInfo.deviceContext ); if( wglMakeCurrent ( appInfo.deviceContext, appInfo.openglRenderContext ) == false ) { printf( "Couldn't make GL context current.\n" ); return -1; } printf("Size of double:%d\n", sizeof( double ) ); glewInit(); MemorySlab gameSlab; gameSlab.slabSize = MEGABYTES( RESERVED_SPACE ); gameSlab.slabStart = VirtualAlloc( NULL, gameSlab.slabSize, MEM_COMMIT | MEM_RESERVE, PAGE_EXECUTE_READWRITE ); assert( gameSlab.slabStart != NULL ); gameSlab.current = gameSlab.slabStart; SlabSubsection_Stack systemsMemory = CarveNewSubsection( &gameSlab, KILOBYTES( 12 ) ); SlabSubsection_Stack gameMemoryStack = CarveNewSubsection( &gameSlab, sizeof( GameMemory ) * 2 ); void* gMemPtr = AllocOnSubStack_Aligned( &gameMemoryStack, sizeof( GameMemory ) ); SoundSystemStorage* soundSystemStorage = Win32InitSound( appInfo.hwnd, 60, &systemsMemory ); RendererStorage* renderSystemStorage = InitRenderer( SCREEN_WIDTH, SCREEN_HEIGHT, &systemsMemory ); SetWindowLong( appInfo.hwnd, GWL_STYLE, 0 ); ShowWindow ( appInfo.hwnd, SW_SHOWNORMAL ); UpdateWindow( appInfo.hwnd ); BOOL canSupportHiResTimer = QueryPerformanceFrequency( &appInfo.timerResolution ); assert( canSupportHiResTimer ); GameInit( &gameSlab, gMemPtr, renderSystemStorage ); MSG Msg; do { while( PeekMessage( &Msg, NULL, 0, 0, PM_REMOVE ) ) { TranslateMessage( &Msg ); DispatchMessage( &Msg ); } static LARGE_INTEGER startTime; LARGE_INTEGER lastTime = startTime; QueryPerformanceCounter( &startTime ); //GAME LOOP if(appInfo.running) { XINPUT_STATE state; DWORD queryResult; memset( &state, 0, sizeof( XINPUT_STATE ) ) ; queryResult = XInputGetState( 0, &state ); if( queryResult == ERROR_SUCCESS ) { //Note: polling of the sticks results in the range not quite reaching 1.0 in the positive direction //it is like this to avoid branching on greater or less than 0.0 appInfo.controllerState.leftStick_x = ((float)state.Gamepad.sThumbLX / 32768.0f ); appInfo.controllerState.leftStick_y = ((float)state.Gamepad.sThumbLY / 32768.0f ); appInfo.controllerState.rightStick_x = ((float)state.Gamepad.sThumbRX / 32768.0f ); appInfo.controllerState.rightStick_y = ((float)state.Gamepad.sThumbRY / 32768.0f ); appInfo.controllerState.leftTrigger = ((float)state.Gamepad.bLeftTrigger / 255.0f ); appInfo.controllerState.rightTrigger = ((float)state.Gamepad.bRightTrigger / 255.0f ); appInfo.controllerState.leftBumper = state.Gamepad.wButtons & XINPUT_GAMEPAD_LEFT_SHOULDER; appInfo.controllerState.rightBumper = state.Gamepad.wButtons & XINPUT_GAMEPAD_RIGHT_SHOULDER; appInfo.controllerState.button1 = state.Gamepad.wButtons & XINPUT_GAMEPAD_A; appInfo.controllerState.button2 = state.Gamepad.wButtons & XINPUT_GAMEPAD_B; appInfo.controllerState.button3 = state.Gamepad.wButtons & XINPUT_GAMEPAD_X; appInfo.controllerState.button4 = state.Gamepad.wButtons & XINPUT_GAMEPAD_Y; appInfo.controllerState.specialButtonLeft = state.Gamepad.wButtons & XINPUT_GAMEPAD_BACK; appInfo.controllerState.specialButtonRight = state.Gamepad.wButtons & XINPUT_GAMEPAD_START; } else { appInfo.controllerState = { }; } LARGE_INTEGER elapsedTime; elapsedTime.QuadPart = startTime.QuadPart - lastTime.QuadPart; elapsedTime.QuadPart *= 1000; elapsedTime.QuadPart /= appInfo.timerResolution.QuadPart; appInfo.running = Update( gMemPtr, (float)elapsedTime.QuadPart, &soundSystemStorage->srb, soundSystemStorage->activeSounds ); Render( gMemPtr, renderSystemStorage ); PushAudioToSoundCard( soundSystemStorage ); SwapBuffers( appInfo.deviceContext ); glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ); } LARGE_INTEGER endTime, computeTime; QueryPerformanceCounter( &endTime ); computeTime.QuadPart = endTime.QuadPart - startTime.QuadPart; computeTime.QuadPart *= 1000; computeTime.QuadPart /= appInfo.timerResolution.QuadPart; if( computeTime.QuadPart <= appInfo.mSecsPerFrame ) { Sleep(appInfo.mSecsPerFrame - computeTime.QuadPart ); } else { printf("Didn't sleep, compute was %ld\n", computeTime.QuadPart ); } } while( appInfo.running ); FreeConsole(); return Msg.wParam; }
int main(int argc, char* argv[]) { if(SDL_Init(SDL_INIT_EVERYTHING) < 0) { std::cout << "Could not init SDL 2" << std::endl; return 0; } SDL_Window *mainWindow = SDL_CreateWindow("Game", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, WINDOW_WIDTH, WINDOW_HEIGHT, SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN); if(!mainWindow) { std::cout << "Could not create a window" << std::endl; return 0; } SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE); SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3); SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 3); SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); SDL_GLContext mainContext = SDL_GL_CreateContext(mainWindow); SDL_GL_SetSwapInterval(1); int value = 0; SDL_GL_GetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, &value); std::cout << "SDL_GL_CONTEXT_MAJOR_VERSION: " << value << std::endl; SDL_GL_GetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, &value); std::cout << "SDL_GL_CONTEXT_MINOR_VERSION: " << value << std::endl; struct stat statBuffer = {}; bool loop = true; GameState gameState = {}; gameState.memory.mainMemorySz = MEGABYTES(128); gameState.memory.mainMemory = mmap(0, gameState.memory.mainMemorySz, PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_PRIVATE, -1, 0); gameState.memory.transientMemorySz = GIGABYTES(2); gameState.memory.transientMemory = mmap(0, gameState.memory.transientMemorySz, PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_PRIVATE, -1, 0); int currentTime = SDL_GetTicks(); while(loop) { int lastTime = currentTime; currentTime = SDL_GetTicks(); int elapsedTime = currentTime - lastTime; // NOTE(Brett): Check for the libgame.so and load the newest verion of the functions stat("libgame.so", &statBuffer); if(LastLibBuildTime == 0 || (statBuffer.st_mtime > LastLibBuildTime)) { if(LibHandle) dlclose(LibHandle); LibHandle = dlopen("libgame.so", RTLD_GLOBAL); GameUpdateAndRender = (update_method)dlsym(LibHandle, "UpdateAndRender"); GameSetup = (setup_method)dlsym(LibHandle, "Setup"); LastLibBuildTime = (int)statBuffer.st_mtime; gameState.initialized = false; GameSetup(&gameState); } SDL_Event event; while(SDL_PollEvent(&event)) { if(event.type == SDL_QUIT) loop = false; if(event.type == SDL_KEYDOWN) { switch(event.key.keysym.sym) { case SDLK_ESCAPE: loop = false; break; default: break; } } } GameUpdateAndRender(&gameState); SDL_GL_SwapWindow(mainWindow); } return 0; }
void gt_ft_trimstat_out(const GtFtTrimstat *trimstat,bool verbose) { if (trimstat != NULL) { printf("died_out=" GT_WU "\t",trimstat->diedout); if (trimstat->distance_dist.nextfreeGtUword > 0) { printf("mean_valid=%.2f\t", trimstat->sum_meanvalid/trimstat->distance_dist.nextfreeGtUword); printf("frontspace=%.2f\n", MEGABYTES((double) trimstat->spaceforfront_total/ trimstat->distance_dist.nextfreeGtUword)); } else { printf("mean_valid=undef\t"); printf("frontspace=undef\n"); } if (verbose) { GtUword idx, count = 1UL, matchlength_sum = 0, matchlength_cum = 0; for (idx = 0; idx <= 100UL; idx++) { matchlength_sum += trimstat->matchlength_dist[idx]; } for (idx = 0; idx <= 100UL; idx++) { if (trimstat->matchlength_dist[idx] > 0) { matchlength_cum += trimstat->matchlength_dist[idx]; printf("# matchlength%s" GT_WU ": " GT_WU " times, " "total=" GT_WU " (%.2f), " "cum=%.2f%%\n", idx < 100UL ? "=" : ">=", idx,trimstat->matchlength_dist[idx], idx * trimstat->matchlength_dist[idx], (double) trimstat->matchlength_dist[idx]/matchlength_sum, 100.0 * (double) matchlength_cum/matchlength_sum); } } for (idx = 0; idx <= 100UL; idx++) { if (trimstat->trim_dist[idx] > 0) { printf("# trim by " GT_WU "%%: " GT_WU " times\n", idx,trimstat->trim_dist[idx]); } } qsort(trimstat->distance_dist.spaceGtUword, trimstat->distance_dist.nextfreeGtUword, sizeof *trimstat->distance_dist.spaceGtUword, gt_ft_trimstat_compare_GtUword); if (trimstat->distance_dist.nextfreeGtUword > 0) { GtUword previous = trimstat->distance_dist.spaceGtUword[0]; for (idx = 1UL; idx < trimstat->distance_dist.nextfreeGtUword; idx++) { if (previous == trimstat->distance_dist.spaceGtUword[idx]) { count++; } else { printf("distance " GT_WU ": " GT_WU " times\n",previous,count); count = 1UL; previous = trimstat->distance_dist.spaceGtUword[idx]; } } printf("distance " GT_WU ": " GT_WU " times\n",previous,count); } for (idx = 0; idx < trimstat->maxvalid_dist.allocatedGtUword; idx++) { if (trimstat->maxvalid_dist.spaceGtUword[idx] > 0) { printf("maxvalid=" GT_WU ": " GT_WU " times\n",idx, trimstat->maxvalid_dist.spaceGtUword[idx]); } } } } }
void showspace(void) { fprintf(stderr,"# space peak in megabytes: %.2f\n",MEGABYTES(spacepeak)); }
static int APIENTRY WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow ) { AllocConsole(); //create a console freopen( "conin$","r",stdin ); freopen( "conout$","w",stdout ); freopen( "conout$","w",stderr ); printf( "Program Started, console initialized\n" ); const char WindowName[] = "Wet Clay"; int64 mSecsPerFrame = 16; //60FPS System system = { }; system.windowHeight = 680; system.windowWidth = 1080; system.ReadWholeFile = &ReadWholeFile; system.GetMostRecentMatchingFile = &GetMostRecentMatchingFile; system.TrackFileUpdates = &TrackFileUpdates; system.DidFileUpdate = &DidFileUpdate; system.WriteFile = &WriteFile; system.HasFocus = &HasFocus; //Center position of window uint16 fullScreenWidth = GetSystemMetrics( SM_CXSCREEN ); uint16 fullScreenHeight = GetSystemMetrics( SM_CYSCREEN ); uint16 windowPosX = ( fullScreenWidth / 2 ) - (system.windowWidth / 2 ); uint16 windowPosY = ( fullScreenHeight / 2 ) - (system.windowHeight / 2 ); WNDCLASSEX wc = { }; wc.cbSize = sizeof(WNDCLASSEX); wc.style = CS_OWNDC; wc.lpfnWndProc = WndProc; wc.cbClsExtra = 0; wc.cbWndExtra = 0; wc.hInstance = hInstance; wc.hIcon = LoadIcon(NULL, IDI_APPLICATION); wc.hCursor = LoadCursor(NULL, IDC_ARROW); wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1); wc.lpszMenuName = NULL; wc.lpszClassName = WindowName; wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION); if( !RegisterClassEx( &wc ) ) { printf( "Failed to register class\n" ); return -1; } EnableCrashingOnCrashes(); // at this point WM_CREATE message is sent/received // the WM_CREATE branch inside WinProc function will execute here HWND hwnd = CreateWindowEx( 0, WindowName, "Wet Clay App", WS_BORDER, windowPosX, windowPosY, system.windowWidth, system.windowHeight, NULL, NULL, hInstance, NULL ); RECT windowRect = { }; GetClientRect( hwnd, &windowRect ); SetWindowLong( hwnd, GWL_STYLE, 0 ); ShowWindow ( hwnd, SW_SHOWNORMAL ); UpdateWindow( hwnd ); LARGE_INTEGER timerResolution; BOOL canSupportHiResTimer = QueryPerformanceFrequency( &timerResolution ); assert( canSupportHiResTimer ); size_t systemsMemorySize = MEGABYTES( 8 ); Stack gameSlab; gameSlab.size = SIZEOF_GLOBAL_HEAP + systemsMemorySize; gameSlab.start = VirtualAlloc( NULL, gameSlab.size, MEM_COMMIT | MEM_RESERVE, PAGE_EXECUTE_READWRITE ); gameSlab.current = gameSlab.start; assert( gameSlab.start != NULL ); Stack systemsMemory = AllocateNewStackFromStack( &gameSlab, systemsMemorySize ); InitWin32WorkerThread( &systemsMemory ); GLRendererGlobalState glRendererStorage = { }; globalGLRendererStorageRef = &glRendererStorage; GLRenderDriver glDriver = Win32InitGLRenderer( hwnd, &system, globalGLRendererStorageRef ); Win32Sound win32Sound = Win32InitSound( hwnd, 60, &systemsMemory ); void* imguistate = InitImGui_LimeStone( &systemsMemory, (RenderDriver*)&glDriver, system.windowWidth, system.windowHeight ); printf( "Remaining System Memory: %Id\n", SPACE_IN_STACK( (&gameSlab) ) ); LoadGameCode( &gameapp ); assert( gameapp.GameInit != NULL ); fileTracking = { }; fileTracking.writeHead = &fileTracking.stringBuffer[0]; int dllTrackingIndex = TrackFileUpdates( GAME_CODE_FILEPATH ); assert( dllTrackingIndex != -1 ); void* gameMemoryPtr = gameapp.GameInit( &gameSlab, (RenderDriver*)&glDriver, &win32Sound.driver, &system ); appIsRunning = true; InputState inputSnapshot1 = { }; InputState inputSnapshot2 = { }; inputSnapshot1.prevState = &inputSnapshot2; inputSnapshot2.prevState = &inputSnapshot1; InputState* currentSnapshotStorage = &inputSnapshot1; MSG Msg; while( appIsRunning ) { if( DidFileUpdate( dllTrackingIndex ) ) { LoadGameCode( &gameapp ); } static LARGE_INTEGER startTime; LARGE_INTEGER elapsedTime; LARGE_INTEGER lastTime = startTime; QueryPerformanceCounter( &startTime ); elapsedTime.QuadPart = startTime.QuadPart - lastTime.QuadPart; elapsedTime.QuadPart *= 1000; elapsedTime.QuadPart /= timerResolution.QuadPart; //GAME LOOP if( gameapp.UpdateAndRender != NULL && gameapp.MixSound != NULL ) { currentSnapshotStorage = currentSnapshotStorage->prevState; QueryInput( system.windowWidth, system.windowHeight, windowPosX, windowPosY, currentSnapshotStorage ); keypressHistoryIndex = 0; UpdateImgui( currentSnapshotStorage, imguistate, system.windowWidth, system.windowHeight ); appIsRunning = gameapp.UpdateAndRender( gameMemoryPtr, (float)elapsedTime.QuadPart, currentSnapshotStorage, &win32Sound.driver, (RenderDriver*)&glDriver, &system, imguistate ); PushAudioToSoundCard( &gameapp, &win32Sound ); ImGui::Render(); BOOL swapBufferSuccess = SwapBuffers( deviceContext ); glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ); } else { printf( "Game code was not loaded...\n" ); } //Windows Message pump is here, so if there is a system level Close or //Destory command, the UpdateAndRender method does not reset the appIsRunning //flag to true while( PeekMessage( &Msg, NULL, 0, 0, PM_REMOVE ) ) { TranslateMessage( &Msg ); DispatchMessage( &Msg ); } //End loop timer query LARGE_INTEGER endTime, computeTime; { QueryPerformanceCounter( &endTime ); computeTime.QuadPart = endTime.QuadPart - startTime.QuadPart; computeTime.QuadPart *= 1000; computeTime.QuadPart /= timerResolution.QuadPart; } if( computeTime.QuadPart <= mSecsPerFrame ) { Sleep(mSecsPerFrame - computeTime.QuadPart ); } else { printf("Didn't sleep, compute was %ld\n", computeTime.QuadPart ); } }; FreeConsole(); return Msg.wParam; }