Exemplo n.º 1
0
int main(int /*argc*/, char* /*argv*/[])
{
	printf("Platform: SDL\n");

	State state;
	ZERO(state);

	if(SDL_Init(SDL_INIT_VIDEO) == 0)
	{
		state.window = SDL_CreateWindow(
			"Game From Scratch",		// title
			SDL_WINDOWPOS_CENTERED,		// x
			SDL_WINDOWPOS_CENTERED,		// y
			640, 480,			// width, height
			SDL_WINDOW_RESIZABLE);		// flags


		if(state.window)
		{
			state.running = true;

			state.renderer = SDL_CreateRenderer(
				state.window,	// window
				-1,		// driver, -1 for auto-detect
				0);		// flags

			// needs renderer to be initialised
			SDLResizeTexture(&(state.FrameBuffer), state.renderer, 640, 480);

			if(state.renderer)
			{
				SDL_Event event;

				int XOffset = 0;
				int YOffset = 0;

				while(state.running)
				{
					// wait for and handle one event
					// between checks for whether the app should quit
					while(SDL_PollEvent(&event))
					{
						SDLHandleEvent(&state, &event);
					}

					RenderGradient(&(state.FrameBuffer), XOffset, YOffset);
					SDLUpdateWindow(&state);

					XOffset += 1;
					YOffset += 2;
				}
			}
			else
			{
				// create renderer failed
				FAIL();
			}
		}
		else
		{
			// create window failed
			FAIL();
		}
	}
	else
	{
		// SDL init failed
		FAIL();
	}


	#ifdef CLEANUP
	// in reverse order to initialisation
	if(state.FrameBuffer.memory) free(state.FrameBuffer.memory);
	if(state.FrameBuffer.texture) SDL_DestroyTexture(state.FrameBuffer.texture);
	if(state.renderer) SDL_DestroyRenderer(state.renderer);
	if(state.window) SDL_DestroyWindow(state.window);
	SDL_Quit();
	#endif

	return 0;
}
Exemplo n.º 2
0
int
main(void) {
    uint64 PerfCountFrequency = SDL_GetPerformanceFrequency();
    SDL_Event Event;
    SDL_Window *Window;
    SDL_Renderer *Renderer;

    if(SDL_Init(SDL_INIT_VIDEO | SDL_INIT_GAMECONTROLLER | SDL_INIT_HAPTIC) != 0) {
        fprintf(stderr, "Could not initialize SDL: %s\n", SDL_GetError());
        return -1;
    }

    atexit(SDL_Quit);

    int WindowWidth = 1300;
    int WindowHeight = 870;
    int BytesPerPixel = 4;

    Window = SDL_CreateWindow("Echelon",
                              0, 0,
                              WindowWidth, WindowHeight,
                              SDL_WINDOW_RESIZABLE);
    
    if(Window) {
        Renderer = SDL_CreateRenderer(Window, -1, 0);

        if(Renderer) {
            GlobalRunning = true;
            window_dimensions Dimensions = SDLGetWindowDimensions(Window);
            SDLCreateNewTexture(&GlobalBuffer,
                                Renderer,
                                Dimensions.Width, Dimensions.Height,
                                BytesPerPixel);

            uint64 LastCounter = SDL_GetPerformanceCounter();
            uint64 LastCycleCount = _rdtsc();

            real64 DebugTimer = 0;
            real64 FPSTimer = 0;
            real64 UpdateTimer = 0;

            uint32 FPS = 0;
            uint32 UPS = 0;
            
            keyboard_input KeyboardInput = {};
            gamepad_input GamePadInput = {};

            game_code Game = LoadGameCode();

            game_memory GameMemory = {};
            GameMemory.IsInitialized = false;
            GameMemory.PlayRumble = SDLPlayRumble;
            GameMemory.WindowDimensions = SDLGetWindowDimensions(Window);
            GameMemory.PlatformDrawRenderQueue = DrawRenderQueueStub;
            GameMemory.PermanentStorageSize = Megabytes(100);
            GameMemory.PermanentStorage = mmap(0,
                                               GameMemory.PermanentStorageSize,
                                               PROT_READ | PROT_WRITE,
                                               MAP_PRIVATE | MAP_ANONYMOUS,
                                               -1, 0);
            
            GameMemory.TransientStorageSize = Gigabytes(2);
            GameMemory.TransientStorage = mmap(0,
                                               GameMemory.TransientStorageSize,
                                               PROT_READ | PROT_WRITE,
                                               MAP_PRIVATE | MAP_ANONYMOUS,
                                               -1, 0);

            Game.GameInit(&GameMemory);
            
            while(GlobalRunning) {
                // NOTE(Redab): This needs to while loop because we need
                // to handle events as long as they are available.
                while(SDL_PollEvent(&Event)) {
                    SDLHandleEvent(&Event, &Dimensions);
                    SDLHandleUserInput(&Event, &Game, &GameMemory, &KeyboardInput, &GamePadInput);
                }

                uint64 EndCycleCount = _rdtsc();
                uint64 EndCounter = SDL_GetPerformanceCounter();
                uint64 CounterElapsed = EndCounter - LastCounter;
                uint64 CyclesElapsed = EndCycleCount - LastCycleCount;

                // NOTE(Redab): CounterElapsed Contains the number of
                // clock cycles since last check. So we need to divide
                // this by the number of cycles per second which we
                // have in PerCountFrequency. Multiplied by 1000 to
                // get milliseconds.

                real64 SecondsPerFrame = ((real64)CounterElapsed / (real64)PerfCountFrequency);
                real64 MSPerFrame = SecondsPerFrame * 1000.0f;
                
                real64 KCPF = ((real64)CyclesElapsed / (1000.0f));

                FPSTimer += MSPerFrame;
                UpdateTimer += MSPerFrame;
                DebugTimer += MSPerFrame;

                if(UpdateTimer >= (1000.0f / 60.0f)) {
                    GameMemory.WindowDimensions = Dimensions;                        
                    Game.GameUpdate(&GameMemory, &KeyboardInput, &GamePadInput, UpdateTimer / 1000.0f);

                    UPS++;
                    UpdateTimer = 0;
                }

                if(FPSTimer >= (1000.0f / 60.0f)) {
                    SDLGameRender(&GlobalBuffer, &Game, &GameMemory);
                    SDLBlitFrameToWindow(&GlobalBuffer, Renderer);
                    
                    FPS++;
                    FPSTimer = 0;
                }
                
                if(DebugTimer >= 1000.0f) {
                    printf("%.05fms/f, FPS: %d, UPS: %d, %.02fKc/f, Timer: %.02f\n",
                           MSPerFrame, FPS, UPS, KCPF, DebugTimer);

                    FPS = 0;
                    UPS = 0;
                    DebugTimer = 0;
                }

                LastCycleCount = EndCycleCount;
                LastCounter = EndCounter;
            }
        } else {
            printf("Failed to create SDL_Renderer: %s\n", SDL_GetError());
        }
    } else {
        printf("Failed to create SDL_Window: %s\n", SDL_GetError());
    }

    SDL_CloseAudio();
    SDL_Quit();
    return 0;
}