chess_board_sim/main.cpp
2019-11-04 20:03:58 -05:00

75 lines
2.3 KiB
C++

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <SDL.h>
#include <SDL_video.h>
#include <SDL_image.h>
#include <time.h>
int main( int argc, const char* argv[] )
{
SDL_Window * win;
SDL_Surface *image;
Uint32 rmask, gmask, bmask, amask;
const char name[] = "BLAH";
if (SDL_Init(SDL_INIT_EVERYTHING) != 0)
{
SDL_Log("Unable to initialize SDL: %s", SDL_GetError());
return 1;
}
win = SDL_CreateWindow(name, 0, 0, 500, 300, SDL_WINDOW_RESIZABLE);
if (win == NULL) {
// In the case that the window could not be made...
printf("Could not create window: %s\n", SDL_GetError());
return 1;
}
image = SDL_GetWindowSurface(win);
printf("Pixel Width: %d, Pixel Heigth: %d\n", image->w, image->h);
// image = SDL_ConvertSurfaceFormat(image, SDL_PIXELFORMAT_RGB444, 0);
//Update the surface
Uint32 *pixels = (Uint32 *)image->pixels;
SDL_UpdateWindowSurface( win );
bool run = true;
while (run) {
clock_t start_t, end_t;
SDL_Event event;
while (SDL_PollEvent(&event)) {
/* handle your event here */
if(event.type == SDL_MOUSEBUTTONDOWN)
{
if(event.button.button == SDL_BUTTON_MIDDLE) // scroll up
{
run = false;
}
else if(event.button.button == SDL_BUTTON_LEFT) // scroll up
{
//Update the surface
SDL_LockSurface(image);
for (size_t i = 0; i < (500*300); i++) {
pixels[i] += 0xFF;
}
SDL_UnlockSurface(image);
start_t = clock();
SDL_UpdateWindowSurface(win);
end_t = clock();
printf("Time to draw a frame %f \n", ((double)(end_t - start_t)/(double)(CLOCKS_PER_SEC)));
}
}
else if (event.type == SDL_QUIT)
{
run = false;
}
}
/* do some other stuff here -- draw your app, etc. */
}
SDL_FreeSurface(image);
SDL_DestroyWindow(win);
return 0;
}
//g++ main.cpp -o blah `sdl2-config --cflags --libs`