Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define _SCL_SECURE_NO_WARNINGS
- #include <SDL.h>
- #include <stdio.h>
- #include <algorithm>
- //Screen dimension constants
- const int SCREEN_WIDTH = 1920;
- const int SCREEN_HEIGHT = 1080;
- SDL_Window* gWindow;
- SDL_Renderer* gRenderer;
- SDL_Texture* gTexture;
- SDL_Event e;
- uint32_t* gPixelBuffer;
- void* gPixels;
- int gPitch;
- bool gExitFlag;
- Uint64 start;
- Uint64 end;
- Uint64 freq;
- double seconds;
- int main(int argc, char* args[])
- {
- gExitFlag = false;
- gPixels = NULL;
- gPixelBuffer = new uint32_t[SCREEN_WIDTH * SCREEN_HEIGHT];
- std::fill_n(gPixelBuffer, SCREEN_WIDTH * SCREEN_HEIGHT, 0xFFFFFFFF);
- gPitch = SCREEN_WIDTH * 4;
- //Initialize SDL
- if (SDL_Init(SDL_INIT_VIDEO) < 0)
- {
- printf("SDL could not initialize! SDL Error: %s\n", SDL_GetError());
- }
- else
- {
- //Set texture filtering to linear
- if (!SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "1"))
- {
- printf("Warning: Linear texture filtering not enabled!");
- }
- //Create window
- gWindow = SDL_CreateWindow("SDL Tutorial", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
- if (gWindow == NULL)
- {
- printf("Window could not be created! SDL Error: %s\n", SDL_GetError());
- }
- else
- {
- //Create renderer for window
- gRenderer = SDL_CreateRenderer(gWindow, -1, SDL_RENDERER_ACCELERATED);// | SDL_RENDERER_PRESENTVSYNC);
- if (gRenderer == NULL)
- {
- printf("Renderer could not be created! SDL Error: %s\n", SDL_GetError());
- }
- else
- {
- gTexture = SDL_CreateTexture(gRenderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_STREAMING, SCREEN_WIDTH, SCREEN_HEIGHT);
- if (gTexture == NULL)
- {
- printf("Unable to create blank texture! SDL Error: %s\n", SDL_GetError());
- }
- else
- {
- while (!gExitFlag)
- {
- //Handle events on queue
- while (SDL_PollEvent(&e) != 0)
- {
- //User requests quit
- if (e.type == SDL_QUIT)
- {
- gExitFlag = true;
- }
- }
- start = SDL_GetPerformanceCounter();
- SDL_RenderClear(gRenderer);
- //SDL_LockTexture(gTexture, NULL, &gPixels, &gPitch);
- //memcpy(gPixels, gPixelBuffer, gPitch * SCREEN_HEIGHT);
- //SDL_UnlockTexture(gTexture);
- SDL_UpdateTexture(gTexture, NULL, gPixelBuffer, gPitch);
- SDL_RenderCopy(gRenderer, gTexture, NULL, NULL);
- gPixels = NULL;
- SDL_RenderPresent(gRenderer);
- end = SDL_GetPerformanceCounter();
- freq = SDL_GetPerformanceFrequency();
- seconds = (end - start) / static_cast<double>(freq);
- printf("Frame time: %fms\n", seconds * 1000.0);
- }
- }
- }
- }
- }
- //Destroy window
- SDL_DestroyWindow(gWindow);
- SDL_DestroyRenderer(gRenderer);
- SDL_DestroyTexture(gTexture);
- delete gPixelBuffer;
- //Quit SDL subsystems
- SDL_Quit();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement