95 lines
2.6 KiB
C++
95 lines
2.6 KiB
C++
#include <iostream>
|
|
#include "SDL2/SDL.h"
|
|
|
|
int main(int argc, char const *argv[]) {
|
|
if (SDL_Init(SDL_INIT_VIDEO) != 0) {
|
|
std::cout << "SDL_Init Error: " << SDL_GetError() << std::endl;
|
|
return 1;
|
|
}
|
|
|
|
SDL_Window *window = SDL_CreateWindow(
|
|
"Breakout", // Title
|
|
100, 100, // X, Y
|
|
640, 480, // Width, Height
|
|
SDL_WINDOW_SHOWN // Visibility
|
|
);
|
|
if (window == nullptr) {
|
|
std::cout << "SDL_CreateWindow Error: " << SDL_GetError() << std::endl;
|
|
SDL_Quit();
|
|
return 1;
|
|
}
|
|
|
|
SDL_Renderer *renderer = SDL_CreateRenderer(
|
|
window, // Window
|
|
-1, // Video driver. -1 means "any compatible"
|
|
SDL_RENDERER_ACCELERATED|SDL_RENDERER_PRESENTVSYNC // Options
|
|
);
|
|
if (renderer == nullptr) {
|
|
SDL_DestroyWindow(window);
|
|
std::cout << "SDL_CreateRenderer Error: " << SDL_GetError() << std::endl;
|
|
SDL_Quit();
|
|
return 1;
|
|
}
|
|
|
|
// Setting background color to black
|
|
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
|
|
SDL_RenderClear(renderer);
|
|
|
|
// Creat a rect at pos ( 50, 50 ) that's 50 pixels wide and 50 pixels high.
|
|
SDL_Rect platform = {
|
|
.x = (640 - 200) / 2,
|
|
.y = (480 - 20),
|
|
.w = 200,
|
|
.h = 20
|
|
};
|
|
|
|
// Set render color to blue ( rect will be rendered in this color )
|
|
SDL_SetRenderDrawColor(renderer, 0, 0, 255, 255);
|
|
|
|
// Render rect
|
|
SDL_RenderFillRect(renderer, &platform);
|
|
|
|
// Render the rect to the screen
|
|
SDL_RenderPresent(renderer);
|
|
|
|
SDL_Event e;
|
|
bool quit = false;
|
|
while (!quit) {
|
|
while (SDL_PollEvent(&e)) {
|
|
if (e.type == SDL_QUIT) {
|
|
quit = true;
|
|
}
|
|
if (e.type == SDL_KEYDOWN) {
|
|
switch (e.key.keysym.sym) {
|
|
case SDLK_q:
|
|
quit = true;
|
|
break;
|
|
case SDLK_LEFT:
|
|
platform.x-=5;
|
|
break;
|
|
case SDLK_RIGHT:
|
|
platform.x+=5;
|
|
break;
|
|
}
|
|
}
|
|
if (e.type == SDL_MOUSEMOTION) {
|
|
platform.x = e.motion.x - 100;
|
|
}
|
|
}
|
|
|
|
// Re-rendering
|
|
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
|
|
SDL_RenderClear(renderer);
|
|
SDL_SetRenderDrawColor(renderer, 0, 0, 255, 255);
|
|
SDL_RenderFillRect(renderer, &platform);
|
|
SDL_RenderPresent(renderer);
|
|
SDL_Delay(1);
|
|
}
|
|
|
|
SDL_DestroyWindow(window);
|
|
SDL_Quit();
|
|
|
|
std::cout << "Exiting nicely" << std::endl;
|
|
return 0;
|
|
}
|