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 AllocatorTest(void*args) { LinearAllocator linearAlloc(KILOBYTES(2), TaggedHeap::Instance().GetMemoryBlock("Gameplay")); //; TestStruct * te[20]; //= (TestShit*)linearAlloc.Allocate(sizeof(TestShit)); Job jArray[20]; for (int i = 0; i < 20; ++i) { te[i] = (TestStruct*)linearAlloc.Allocate(sizeof(TestStruct)); jArray[i].InitialiseJob(MoveTestShit, te[i], 0, "testing"); } JobCounter counter = JobScheduler::Instance().AddJob(20, jArray); JobScheduler::Instance().WaitForCounter(counter, 20); TestStruct finalResult = { 100, 100 }; for (int i = 0; i < 20; ++i) { ASSERT_EQ(finalResult, *te[i]); } linearAlloc.Free(); }
// private //------------------------------------------------------------------------- Hash HandleCache::_CalculateHash( HCURSOR hcursor ) { static const ULong HASH_BUFFER_LIMIT = KILOBYTES(8); ICONINFO iconinfo = {0}; LONG count; Byte buffer[HASH_BUFFER_LIMIT]; // require valid if( hcursor == NULL ) return 0; // get icon info if( GetIconInfo(hcursor, &iconinfo) == FALSE ) return 0; // get icon bitmap buffer count = GetBitmapBits( iconinfo.hbmColor ? iconinfo.hbmColor : iconinfo.hbmMask, sizeof(buffer), buffer ); // iconinfo cleanup if( iconinfo.hbmColor ) DeleteObject(iconinfo.hbmColor); if( iconinfo.hbmMask ) DeleteObject(iconinfo.hbmMask); // fail if no bits read if(count == 0) return 0; // generate hash return Tools::Fnv164Hash(buffer, count); }
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); } }
namespace cmd_buffer { static const size_t COMMAND_BUFFER_SIZE = KILOBYTES(16); static char g_cmd_buffer[COMMAND_BUFFER_SIZE]; static uint32_t g_cmd_length = 0; void add_text(const char * const text) { const size_t len = strlen(text); if (g_cmd_length + len >= COMMAND_BUFFER_SIZE) { return; } memcpy(g_cmd_buffer + g_cmd_length, text, len); g_cmd_length += len; } void execute_buffer() { while (g_cmd_length) { // find a line break character size_t i = 0u; uint32_t quotes = 0; for (i = 0u; i < g_cmd_length; ++i) { if (g_cmd_buffer[i] == '"') { ++quotes; } if (g_cmd_buffer[i] == '\n' || g_cmd_buffer[i] == '\r') { break; } } g_cmd_buffer[i] = '\0'; std::string cmd_text(g_cmd_buffer); //shift everything down the buffer as commands may add new text to the //buffer. if (g_cmd_length == i) { //nothing left to move, just reset the size g_cmd_length = 0; } else { ++i; g_cmd_length -= i; std::memmove(g_cmd_buffer, g_cmd_buffer + i, g_cmd_length); } //now execute cmd::execute(cmd_text); } } } // namespace cmd_buffer
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; }