migrat a SDL3
This commit is contained in:
+27
-34
@@ -2,15 +2,15 @@
|
||||
#include "ball.h"
|
||||
#include "defines.h"
|
||||
#include <iostream>
|
||||
#include <SDL3/SDL.h>
|
||||
|
||||
SDL_Window *window = NULL;
|
||||
SDL_Renderer *renderer = NULL;
|
||||
SDL_Event *event;
|
||||
SDL_Window *window = nullptr;
|
||||
SDL_Renderer *renderer = nullptr;
|
||||
Texture *texture = nullptr;
|
||||
Ball *ball[NUM_BALLS];
|
||||
|
||||
bool shouldExit = false;
|
||||
Uint32 ticks = 0;
|
||||
bool should_exit = false;
|
||||
Uint64 ticks = 0;
|
||||
|
||||
bool init()
|
||||
{
|
||||
@@ -18,7 +18,7 @@ bool init()
|
||||
bool success = true;
|
||||
|
||||
// Initialize SDL
|
||||
if (SDL_Init(SDL_INIT_VIDEO) < 0)
|
||||
if (SDL_Init(SDL_INIT_VIDEO))
|
||||
{
|
||||
printf("SDL could not initialize! SDL Error: %s\n", SDL_GetError());
|
||||
success = false;
|
||||
@@ -26,7 +26,7 @@ bool init()
|
||||
else
|
||||
{
|
||||
// Create window
|
||||
window = SDL_CreateWindow("demo_pelotas1", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH * 2, SCREEN_HEIGHT * 2, SDL_WINDOW_SHOWN);
|
||||
window = SDL_CreateWindow("demo_pelotas1", DEMO_WIDTH * WINDOW_ZOOM, DEMO_HEIGHT * WINDOW_ZOOM, SDL_WINDOW_OPENGL);
|
||||
if (window == NULL)
|
||||
{
|
||||
printf("Window could not be created! SDL Error: %s\n", SDL_GetError());
|
||||
@@ -35,7 +35,7 @@ bool init()
|
||||
else
|
||||
{
|
||||
// Create renderer for window
|
||||
renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
|
||||
renderer = SDL_CreateRenderer(window, nullptr);
|
||||
if (renderer == NULL)
|
||||
{
|
||||
printf("Renderer could not be created! SDL Error: %s\n", SDL_GetError());
|
||||
@@ -46,26 +46,24 @@ bool init()
|
||||
// Initialize renderer color
|
||||
SDL_SetRenderDrawColor(renderer, 0xFF, 0xFF, 0xFF, 0xFF);
|
||||
|
||||
SDL_RenderSetLogicalSize(renderer, SCREEN_WIDTH, SCREEN_HEIGHT);
|
||||
SDL_SetRenderLogicalPresentation(renderer, DEMO_WIDTH, DEMO_HEIGHT, SDL_LOGICAL_PRESENTATION_INTEGER_SCALE);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
const int VX = rand() % 2 == 0 ? 1 : -1;
|
||||
const int VY = rand() % 2 == 0 ? 1 : -1;
|
||||
const int X = rand() % DEMO_WIDTH;
|
||||
const int Y = rand() % DEMO_HEIGHT;
|
||||
constexpr int SIZE = BALL_SIZE;
|
||||
ball[i] = new Ball(X, Y, SIZE, SIZE, VX, VY, texture);
|
||||
}
|
||||
|
||||
ticks = SDL_GetTicks();
|
||||
srand(time(NULL));
|
||||
srand(time(nullptr));
|
||||
|
||||
return success;
|
||||
}
|
||||
@@ -75,13 +73,6 @@ void close()
|
||||
// Destroy window
|
||||
SDL_DestroyRenderer(renderer);
|
||||
SDL_DestroyWindow(window);
|
||||
window = NULL;
|
||||
renderer = NULL;
|
||||
|
||||
if (event)
|
||||
{
|
||||
delete event;
|
||||
}
|
||||
|
||||
if (texture)
|
||||
{
|
||||
@@ -102,24 +93,26 @@ void close()
|
||||
|
||||
void checkEvents()
|
||||
{
|
||||
SDL_Event event;
|
||||
|
||||
// Comprueba los eventos que hay en la cola
|
||||
while (SDL_PollEvent(event) != 0)
|
||||
while (SDL_PollEvent(&event) != 0)
|
||||
{
|
||||
// Evento de salida de la aplicación
|
||||
if (event->type == SDL_QUIT)
|
||||
if (event.type == SDL_EVENT_QUIT)
|
||||
{
|
||||
shouldExit = true;
|
||||
should_exit = true;
|
||||
break;
|
||||
}
|
||||
|
||||
if (event->type == SDL_KEYDOWN && event->key.repeat == 0)
|
||||
if (event.type == SDL_EVENT_KEY_DOWN && event.key.repeat == 0)
|
||||
{
|
||||
switch (event->key.keysym.sym)
|
||||
switch (event.key.key)
|
||||
{
|
||||
case SDLK_ESCAPE:
|
||||
shouldExit = true;
|
||||
should_exit = true;
|
||||
break;
|
||||
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
@@ -129,7 +122,7 @@ void checkEvents()
|
||||
|
||||
void update()
|
||||
{
|
||||
if (SDL_GetTicks() - ticks > 15)
|
||||
if (SDL_GetTicks() - ticks > DEMO_SPEED)
|
||||
{
|
||||
ticks = SDL_GetTicks();
|
||||
|
||||
@@ -157,7 +150,7 @@ int main(int argc, char *args[])
|
||||
{
|
||||
init();
|
||||
|
||||
while (!shouldExit)
|
||||
while (!should_exit)
|
||||
{
|
||||
update();
|
||||
checkEvents();
|
||||
|
||||
Reference in New Issue
Block a user