migrat a SDL3
This commit is contained in:
240
source/main.cpp
Normal file
240
source/main.cpp
Normal file
@@ -0,0 +1,240 @@
|
||||
#include <SDL3/SDL.h>
|
||||
#include "dot.h"
|
||||
#include "defines.h"
|
||||
#include "dbgtxt.h"
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
|
||||
SDL_Window *window = NULL;
|
||||
SDL_Renderer *renderer = NULL;
|
||||
Dot::DotData dots[MAX_DOTS];
|
||||
SDL_FPoint dot_points[MAX_DOTS];
|
||||
int test[8] = {1, 10, 100, 500, 1000, 10000, 50000, MAX_DOTS};
|
||||
int scenario = 0;
|
||||
std::string text = "";
|
||||
int text_pos = 0;
|
||||
bool show_text = true;
|
||||
int counter = 0;
|
||||
|
||||
bool should_exit = false;
|
||||
Uint32 ticks = 0;
|
||||
|
||||
void setText()
|
||||
{
|
||||
const std::string text2 = test[scenario] == 1 ? " PIXEL" : " PIXELS";
|
||||
text = std::to_string(test[scenario]) + text2;
|
||||
const int size = text.size() * 8;
|
||||
text_pos = WIDTH / 2 - size / 2;
|
||||
counter = TEXT_TIME;
|
||||
show_text = true;
|
||||
}
|
||||
|
||||
void initDots()
|
||||
{
|
||||
for (int i = 0; i < test[scenario]; ++i)
|
||||
{
|
||||
const int sign = ((rand() % 2) * 2) - 1;
|
||||
const float x = (rand() % (WIDTH / 2)) + (WIDTH / 4);
|
||||
const float vx = (((rand() % 20) + 10) * 0.1f) * sign;
|
||||
const float vy = ((rand() % 60) - 30) * 0.1f;
|
||||
const Color color = {(rand() % 192) + 32, (rand() % 192) + 32, (rand() % 192) + 32};
|
||||
dots[i] = Dot::ini(x, vx, vy, color);
|
||||
}
|
||||
setText();
|
||||
}
|
||||
|
||||
void pushUpDots()
|
||||
{
|
||||
for (int i = 0; i < test[scenario]; ++i)
|
||||
{
|
||||
const int sign = ((rand() % 2) * 2) - 1;
|
||||
const float vx = (((rand() % 20) + 10) * 0.1f) * sign;
|
||||
const float vy = ((rand() % 40) * 0.1f) + 5;
|
||||
Dot::modVel(dots[i], vx, -vy);
|
||||
}
|
||||
}
|
||||
|
||||
bool init()
|
||||
{
|
||||
// Initialization flag
|
||||
bool success = true;
|
||||
|
||||
// Initialize SDL
|
||||
if (!SDL_Init(SDL_INIT_VIDEO))
|
||||
{
|
||||
printf("SDL could not initialize! SDL Error: %s\n", SDL_GetError());
|
||||
success = false;
|
||||
}
|
||||
else
|
||||
{
|
||||
// Create window
|
||||
window = SDL_CreateWindow(WINDOW_CAPTION, WIDTH * ZOOM, HEIGHT * ZOOM, SDL_WINDOW_OPENGL);
|
||||
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, nullptr);
|
||||
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_SetRenderLogicalPresentation(renderer, WIDTH, HEIGHT, SDL_LOGICAL_PRESENTATION_INTEGER_SCALE);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
ticks = SDL_GetTicks();
|
||||
srand(time(nullptr));
|
||||
dbg_init(renderer);
|
||||
initDots();
|
||||
setText();
|
||||
|
||||
return success;
|
||||
}
|
||||
|
||||
void close()
|
||||
{
|
||||
// Destroy window
|
||||
SDL_DestroyRenderer(renderer);
|
||||
SDL_DestroyWindow(window);
|
||||
|
||||
// Quit SDL subsystems
|
||||
SDL_Quit();
|
||||
}
|
||||
|
||||
void checkEvents()
|
||||
{
|
||||
SDL_Event event;
|
||||
// Comprueba los eventos que hay en la cola
|
||||
while (SDL_PollEvent(&event) != 0)
|
||||
{
|
||||
// Evento de salida de la aplicación
|
||||
if (event.type == SDL_EVENT_QUIT)
|
||||
{
|
||||
should_exit = true;
|
||||
break;
|
||||
}
|
||||
|
||||
if (event.type == SDL_EVENT_KEY_DOWN && event.key.repeat == 0)
|
||||
{
|
||||
switch (event.key.key)
|
||||
{
|
||||
case SDLK_ESCAPE:
|
||||
should_exit = true;
|
||||
break;
|
||||
|
||||
case SDLK_SPACE:
|
||||
pushUpDots();
|
||||
break;
|
||||
|
||||
case SDLK_1:
|
||||
scenario = 0;
|
||||
initDots();
|
||||
break;
|
||||
|
||||
case SDLK_2:
|
||||
scenario = 1;
|
||||
initDots();
|
||||
break;
|
||||
|
||||
case SDLK_3:
|
||||
scenario = 2;
|
||||
initDots();
|
||||
break;
|
||||
|
||||
case SDLK_4:
|
||||
scenario = 3;
|
||||
initDots();
|
||||
break;
|
||||
|
||||
case SDLK_5:
|
||||
scenario = 4;
|
||||
initDots();
|
||||
break;
|
||||
|
||||
case SDLK_6:
|
||||
scenario = 5;
|
||||
initDots();
|
||||
break;
|
||||
|
||||
case SDLK_7:
|
||||
scenario = 6;
|
||||
initDots();
|
||||
break;
|
||||
|
||||
case SDLK_8:
|
||||
scenario = 7;
|
||||
initDots();
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void update()
|
||||
{
|
||||
if (SDL_GetTicks() - ticks > 15)
|
||||
{
|
||||
ticks = SDL_GetTicks();
|
||||
|
||||
for (int i = 0; i < test[scenario]; ++i)
|
||||
{
|
||||
dot_points[i] = Dot::update(dots[i]);
|
||||
}
|
||||
|
||||
if (counter > 0)
|
||||
{
|
||||
counter--;
|
||||
}
|
||||
else
|
||||
{
|
||||
show_text = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void render()
|
||||
{
|
||||
SDL_SetRenderDrawColor(renderer, 32, 32, 32, 255);
|
||||
SDL_RenderClear(renderer);
|
||||
|
||||
SDL_SetRenderDrawColor(renderer, 224, 224, 224, 255);
|
||||
SDL_RenderPoints(renderer, dot_points, test[scenario]);
|
||||
|
||||
if (show_text)
|
||||
{
|
||||
dbg_print(text_pos, 8, text.c_str(), 255, 32, 32);
|
||||
}
|
||||
|
||||
SDL_RenderPresent(renderer);
|
||||
}
|
||||
|
||||
int main(int argc, char *args[])
|
||||
{
|
||||
init();
|
||||
|
||||
while (!should_exit)
|
||||
{
|
||||
update();
|
||||
checkEvents();
|
||||
render();
|
||||
}
|
||||
|
||||
close();
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user