1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
|
#include <SDL.h>
#include <iostream>
using namespace std;
int main(int argc, char* argv[]) {
// Start SDL2
const int SCREEN_WIDTH = 800;
const int SCREEN_HEIGHT = 600;
if(SDL_Init(SDL_INIT_VIDEO) < 0){
return 1;
}
SDL_Window *window = SDL_CreateWindow("Particle eXplosion",SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED,SCREEN_WIDTH,SCREEN_HEIGHT,SDL_WINDOW_SHOWN);
if(window == NULL){
cout << "window is null" << endl;
SDL_Quit();
return 2;
}
SDL_Renderer *renderer = SDL_CreateRenderer(window,-1,SDL_RENDERER_PRESENTVSYNC);
SDL_Texture *texture = SDL_CreateTexture(renderer,SDL_PIXELFORMAT_RGBA8888,SDL_TEXTUREACCESS_STATIC,SCREEN_WIDTH,SCREEN_HEIGHT);
Uint32 *buffer = new Uint32[SCREEN_WIDTH * SCREEN_HEIGHT];
bool quit = false;
SDL_Event event;
SDL_KeyboardEvent kEvent;
memset(buffer,0xFF,SCREEN_WIDTH * SCREEN_HEIGHT * sizeof(Uint32));
SDL_UpdateTexture(texture,NULL,buffer,SCREEN_WIDTH*sizeof(Uint32));
SDL_RenderClear(renderer);
SDL_RenderCopy(renderer,texture,NULL,NULL);
SDL_RenderPresent(renderer);
bool pressed = false;
while(!quit){
for(int i = 0; i < SCREEN_WIDTH * SCREEN_HEIGHT; i++){
buffer[i] = 0xFFFF00FF;
}
SDL_UpdateTexture(texture,NULL,buffer,SCREEN_WIDTH*sizeof(Uint32));
SDL_RenderClear(renderer);
SDL_RenderCopy(renderer,texture,NULL,NULL);
SDL_RenderPresent(renderer);
if(kEvent.type == SDL_KEYDOWN){
cout << "key down" << endl;
}
for(int i = 0; i < SCREEN_WIDTH * SCREEN_HEIGHT; i++){
if(i % 2 == 0) {
buffer[i] = 0xFFFFFFFF;
} else {
buffer[i] = 0xFF0000FF;
}
}
pressed = false;
SDL_UpdateTexture(texture,NULL,buffer,SCREEN_WIDTH*sizeof(Uint32));
SDL_RenderClear(renderer);
SDL_RenderCopy(renderer,texture,NULL,NULL);
SDL_RenderPresent(renderer);
while(SDL_PollEvent(&event)){
if(event.key.keysym.scancode == SDL_SCANCODE_5 && !pressed){
cout << "5 pressed" << endl;
pressed = true;
}
if(event.type == SDL_QUIT){
quit = true;
}
}
}
delete [] buffer;
SDL_DestroyRenderer(renderer);
SDL_DestroyTexture(texture);
SDL_Delay(9000);
SDL_DestroyWindow(window);
SDL_Quit();
return 0;
}
|