286 lines
4.7 KiB
C++
286 lines
4.7 KiB
C++
#include <SDL2/SDL.h>
|
|
#include "texture.h"
|
|
#include "ball.h"
|
|
#include "defines.h"
|
|
#include "dbgtxt.h"
|
|
#include <iostream>
|
|
#include <vector>
|
|
|
|
SDL_Window *window = nullptr;
|
|
SDL_Renderer *renderer = nullptr;
|
|
SDL_Event *event;
|
|
Texture *texture = nullptr;
|
|
std::vector<Ball *> balls;
|
|
int test[8] = {1, 10, 100, 500, 1000, 10000, 50000, 100000};
|
|
|
|
bool shouldExit = false;
|
|
Uint32 ticks = 0;
|
|
int index = 0;
|
|
std::string text = "";
|
|
int textPos = 0;
|
|
bool showText = true;
|
|
int counter = 0;
|
|
|
|
void deleteBalls();
|
|
|
|
void setText()
|
|
{
|
|
const std::string text2 = test[index] == 1 ? " PELOTA" : " PELOTAS";
|
|
text = std::to_string(test[index]) + text2;
|
|
const int size = text.size() * 8;
|
|
textPos = SCREEN_WIDTH / 2 - size / 2;
|
|
counter = TEXT_TIME;
|
|
showText = true;
|
|
}
|
|
|
|
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);
|
|
}
|
|
setText();
|
|
}
|
|
|
|
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 switchBallsGravity()
|
|
{
|
|
for (auto ball : balls)
|
|
{
|
|
if (ball)
|
|
{
|
|
ball->switchGravity();
|
|
;
|
|
}
|
|
}
|
|
}
|
|
|
|
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
|
|
{
|
|
// Create window
|
|
window = SDL_CreateWindow(WINDOW_CAPTION, SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH * WINDOW_SIZE, SCREEN_HEIGHT * WINDOW_SIZE, SDL_WINDOW_SHOWN);
|
|
if (window == nullptr)
|
|
{
|
|
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 == nullptr)
|
|
{
|
|
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(nullptr));
|
|
dbg_init(renderer);
|
|
initBalls(index);
|
|
|
|
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)
|
|
{
|
|
switch (event->key.keysym.sym)
|
|
{
|
|
case SDLK_ESCAPE:
|
|
shouldExit = true;
|
|
break;
|
|
|
|
case SDLK_SPACE:
|
|
pushUpBalls();
|
|
break;
|
|
|
|
case SDLK_g:
|
|
switchBallsGravity();
|
|
break;
|
|
|
|
case SDLK_1:
|
|
index = 0;
|
|
initBalls(index);
|
|
break;
|
|
|
|
case SDLK_2:
|
|
index = 1;
|
|
initBalls(index);
|
|
break;
|
|
|
|
case SDLK_3:
|
|
index = 2;
|
|
initBalls(index);
|
|
break;
|
|
|
|
case SDLK_4:
|
|
index = 3;
|
|
initBalls(index);
|
|
break;
|
|
|
|
case SDLK_5:
|
|
index = 4;
|
|
initBalls(index);
|
|
break;
|
|
|
|
case SDLK_6:
|
|
index = 5;
|
|
initBalls(index);
|
|
break;
|
|
|
|
case SDLK_7:
|
|
index = 6;
|
|
initBalls(index);
|
|
break;
|
|
|
|
case SDLK_8:
|
|
index = 7;
|
|
initBalls(index);
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
void update()
|
|
{
|
|
if (SDL_GetTicks() - ticks > 15)
|
|
{
|
|
ticks = SDL_GetTicks();
|
|
|
|
for (auto ball : balls)
|
|
{
|
|
ball->update();
|
|
}
|
|
|
|
if (counter > 0)
|
|
counter--;
|
|
else
|
|
showText = false;
|
|
}
|
|
}
|
|
|
|
void render()
|
|
{
|
|
SDL_SetRenderDrawColor(renderer, 32, 32, 32, 255);
|
|
SDL_RenderClear(renderer);
|
|
|
|
for (auto ball : balls)
|
|
{
|
|
ball->render();
|
|
}
|
|
|
|
if (showText)
|
|
dbg_print(textPos, 8, text.c_str(), 255, 255, 255);
|
|
|
|
SDL_RenderPresent(renderer);
|
|
}
|
|
|
|
int main(int argc, char *args[])
|
|
{
|
|
init();
|
|
|
|
while (!shouldExit)
|
|
{
|
|
update();
|
|
checkEvents();
|
|
render();
|
|
}
|
|
|
|
close();
|
|
|
|
return 0;
|
|
} |