#include #include "texture.h" #include "ball.h" #include "defines.h" #include #include SDL_Window *window = NULL; SDL_Renderer *renderer = NULL; SDL_Event *event; Texture *texture = nullptr; std::vector balls; int test[8] = {1, 10, 100, 500, 1000, 10000, 50000, 100000}; bool shouldExit = false; Uint32 ticks = 0; void deleteBalls(); void initBalls(int value) { deleteBalls(); for (int i = 0; i < test[value]; ++i) { const int sign = ((rand() % 2) * 2) - 1; ; const float x = (rand() % (SCREEN_WIDTH / 2)) + (SCREEN_WIDTH / 4); const float vx = (((rand() % 20) + 10) * 0.1f) * sign; const float vy = ((rand() % 60) - 30) * 0.1f; const color_t color = {(rand() % 192) + 32, (rand() % 192) + 32, (rand() % 192) + 32}; Ball *b = new Ball(x, vx, vy, color, texture); balls.push_back(b); } } void pushUpBalls() { for (auto ball : balls) { const int sign = ((rand() % 2) * 2) - 1; const float vx = (((rand() % 20) + 10) * 0.1f) * sign; const float vy = ((rand() % 40) * 0.1f) + 5; ball->modVel(vx, -vy); } } void deleteBalls() { for (auto ball : balls) { if (ball) { delete ball; ball = nullptr; } } balls.clear(); } bool init() { // Initialization flag bool success = true; // Initialize SDL if (SDL_Init(SDL_INIT_VIDEO) < 0) { printf("SDL could not initialize! SDL Error: %s\n", SDL_GetError()); success = false; } else { // Set texture filtering to linear if (!SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "1")) { printf("Warning: Linear texture filtering not enabled!"); } // Create window window = SDL_CreateWindow(WINDOW_CAPTION, SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH * 2, SCREEN_HEIGHT * 2, SDL_WINDOW_SHOWN); if (window == NULL) { printf("Window could not be created! SDL Error: %s\n", SDL_GetError()); success = false; } else { // Create renderer for window renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED); if (renderer == NULL) { printf("Renderer could not be created! SDL Error: %s\n", SDL_GetError()); success = false; } else { // Initialize renderer color SDL_SetRenderDrawColor(renderer, 0xFF, 0xFF, 0xFF, 0xFF); // Establece el tamaño del renderizador SDL_RenderSetLogicalSize(renderer, SCREEN_WIDTH, SCREEN_HEIGHT); } } } event = new SDL_Event(); texture = new Texture(renderer, "resources/pelota.png"); ticks = SDL_GetTicks(); srand(time(NULL)); initBalls(2); return success; } void close() { // Destroy window SDL_DestroyRenderer(renderer); SDL_DestroyWindow(window); window = nullptr; renderer = nullptr; if (event) { delete event; event = nullptr; } if (texture) { delete texture; texture = nullptr; } deleteBalls(); // Quit SDL subsystems SDL_Quit(); } void checkEvents() { // Comprueba los eventos que hay en la cola while (SDL_PollEvent(event) != 0) { // Evento de salida de la aplicación if (event->type == SDL_QUIT) { shouldExit = true; break; } if (event->type == SDL_KEYDOWN && event->key.repeat == 0) { if (event->key.keysym.sym == SDLK_SPACE) { pushUpBalls(); } if (event->key.keysym.sym == SDLK_1) { initBalls(0); } if (event->key.keysym.sym == SDLK_2) { initBalls(1); } if (event->key.keysym.sym == SDLK_3) { initBalls(2); } if (event->key.keysym.sym == SDLK_4) { initBalls(3); } if (event->key.keysym.sym == SDLK_5) { initBalls(4); } if (event->key.keysym.sym == SDLK_6) { initBalls(5); } if (event->key.keysym.sym == SDLK_7) { initBalls(6); } if (event->key.keysym.sym == SDLK_8) { initBalls(7); } } } } void update() { if (SDL_GetTicks() - ticks > 15) { ticks = SDL_GetTicks(); for (auto ball : balls) { ball->update(); } } } void render() { SDL_SetRenderDrawColor(renderer, 32, 32, 32, 255); SDL_RenderClear(renderer); for (auto ball : balls) { ball->render(); } SDL_RenderPresent(renderer); } int main(int argc, char *args[]) { init(); while (!shouldExit) { update(); checkEvents(); render(); } close(); return 0; }