170 lines
2.8 KiB
C++
170 lines
2.8 KiB
C++
#include "texture.h"
|
|
#include "ball.h"
|
|
#include "defines.h"
|
|
#include <iostream>
|
|
|
|
SDL_Window *window = NULL;
|
|
SDL_Renderer *renderer = NULL;
|
|
SDL_Event *event;
|
|
Texture *texture = nullptr;
|
|
Ball *ball[NUM_BALLS];
|
|
|
|
bool shouldExit = false;
|
|
Uint32 ticks = 0;
|
|
|
|
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("demo_pelotas1", 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);
|
|
|
|
SDL_RenderSetLogicalSize(renderer, SCREEN_WIDTH, SCREEN_HEIGHT);
|
|
}
|
|
}
|
|
}
|
|
|
|
event = new SDL_Event();
|
|
|
|
texture = new Texture(renderer, TEXTURE_FILE);
|
|
for (int i = 0; i < NUM_BALLS; ++i)
|
|
{
|
|
const int vx = rand() % 2 == 0 ? 1 : -1;
|
|
const int vy = rand() % 2 == 0 ? 1 : -1;
|
|
const int x = rand() % SCREEN_WIDTH;
|
|
const int y = rand() % SCREEN_HEIGHT;
|
|
const int size = BALL_SIZE;
|
|
ball[i] = new Ball(x, y, size, size, vx, vy, texture);
|
|
}
|
|
|
|
ticks = SDL_GetTicks();
|
|
srand(time(NULL));
|
|
|
|
return success;
|
|
}
|
|
|
|
void close()
|
|
{
|
|
// Destroy window
|
|
SDL_DestroyRenderer(renderer);
|
|
SDL_DestroyWindow(window);
|
|
window = NULL;
|
|
renderer = NULL;
|
|
|
|
if (event)
|
|
{
|
|
delete event;
|
|
}
|
|
|
|
if (texture)
|
|
{
|
|
delete texture;
|
|
}
|
|
|
|
for (int i = 0; i < NUM_BALLS; ++i)
|
|
{
|
|
if (ball[i])
|
|
{
|
|
delete ball[i];
|
|
}
|
|
}
|
|
|
|
// 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;
|
|
|
|
default:
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
void update()
|
|
{
|
|
if (SDL_GetTicks() - ticks > 15)
|
|
{
|
|
ticks = SDL_GetTicks();
|
|
|
|
for (int i = 0; i < NUM_BALLS; ++i)
|
|
{
|
|
ball[i]->update();
|
|
}
|
|
}
|
|
}
|
|
|
|
void render()
|
|
{
|
|
SDL_SetRenderDrawColor(renderer, BG_R, BG_G, BG_B, 255);
|
|
SDL_RenderClear(renderer);
|
|
|
|
for (int i = 0; i < NUM_BALLS; ++i)
|
|
{
|
|
ball[i]->render();
|
|
}
|
|
|
|
SDL_RenderPresent(renderer);
|
|
}
|
|
|
|
int main(int argc, char *args[])
|
|
{
|
|
init();
|
|
|
|
while (!shouldExit)
|
|
{
|
|
update();
|
|
checkEvents();
|
|
render();
|
|
}
|
|
|
|
close();
|
|
|
|
return 0;
|
|
} |