Created
April 5, 2019 03:09
-
-
Save cuu/105b9160bf8c670e2dca85dab06c2b82 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <SDL2/SDL.h> | |
int main() { | |
int windowHeight = 240; | |
int windowWidth = 320; | |
if (SDL_Init(SDL_INIT_VIDEO) < 0) { | |
printf("Initialization failed\n"); | |
return 1; | |
} | |
SDL_Window *window = SDL_CreateWindow("Practice making sdl Window", | |
SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, windowWidth, | |
windowHeight, SDL_WINDOW_SHOWN); | |
if (window == NULL) { | |
SDL_Quit(); | |
return 2; | |
} | |
// We create a renderer with hardware acceleration, we also present according with the vertical sync refresh. | |
SDL_Renderer *s = SDL_CreateRenderer(window, 0, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC) ; | |
SDL_Texture * texture = SDL_CreateTexture(s, | |
SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STATIC, windowWidth, windowHeight); | |
int quit = 0; | |
SDL_Event event; | |
int x=0; | |
int y=0; | |
int color = 0; | |
int frames = 0; | |
int fps = 0; | |
int prev_time = SDL_GetTicks(); | |
int current_time = SDL_GetTicks(); | |
int *pixels = (int*)malloc(windowWidth*windowHeight*sizeof(int)); | |
int addr= 0; | |
memset(pixels, 255, windowWidth * windowHeight * sizeof(int)); | |
while (!quit) { | |
while (SDL_PollEvent(&event)) { | |
if (event.type == SDL_QUIT) { | |
quit = 1; | |
} | |
if(event.type == SDL_KEYDOWN) { | |
if ( event.key.keysym.sym == SDLK_q) { | |
printf("Quiting...\n"); | |
quit=1; | |
break; | |
} | |
} | |
} | |
for(y=0;y<windowHeight;y++) { | |
for(x=0;x<windowWidth;x++) { | |
addr = x +y*windowWidth; | |
pixels[addr] = rand()%255; | |
} | |
} | |
SDL_UpdateTexture(texture, NULL, pixels, windowWidth * sizeof(int)); | |
/* | |
SDL_RenderClear(s); | |
for (x=0;x<windowWidth;x++) { | |
for(y=0;y<windowHeight;y++) { | |
color = rand()%0xff; | |
SDL_SetRenderDrawColor(s, color,color,color, 0xFF); | |
SDL_RenderDrawPoint(s,x,y); | |
} | |
} | |
SDL_SetRenderDrawColor(s, 0x00, 0x00, 0x00, 0xFF); | |
*/ | |
SDL_RenderClear(s); | |
SDL_RenderCopy(s, texture, NULL, NULL); | |
SDL_RenderPresent(s); | |
frames++; | |
current_time = SDL_GetTicks(); | |
if( (current_time - prev_time) > 10000) { | |
fps = frames/10; | |
printf("fps is %d\n",fps); | |
frames = 0; | |
prev_time = current_time; | |
} | |
} | |
SDL_DestroyWindow(window); | |
SDL_DestroyRenderer(s); | |
SDL_Quit(); | |
free(pixels); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment