demo: convertido todo el codigo que estava a lo bruto en el main a funciones y procedimientos

This commit is contained in:
2023-05-24 17:43:38 +02:00
parent 51c0a97a8a
commit 83d230846e

324
main.cpp
View File

@@ -19,16 +19,87 @@ Código fuente creado por JailDesigner
SDL_Event *event;
SDL_Window *window;
SDL_Renderer *renderer;
Asset *asset;
JA_Music_t *music;
JA_Sound_t *sound;
Input *input;
struct options_t *options;
Screen *screen;
Text *text;
Text *debugText;
Texture *texture;
MovingSprite *sprite;
Uint32 ticks = 0;
Uint32 ticksSpeed = 15;
bool should_exit = false;
int counter = 0;
int gradColorMin = 64; // Minimo color más alto del degradado
int gradColorMax = 192; // Minimo color más alto del degradado
int gradCurrentColor = 192; // Color actual más alto del degradado
int gradBreathDirection = 0; // Indica si gradCurrentColor crece o decrece
struct options_t *options;
Asset *asset;
SDL_Window *window;
string controllerName;
string inputPressed;
// Inicializa las opciones
void initOptions();
// Inicializa la lista de recursos
void initAsset(char *argv[]);
// Inicializa SDL
void initSDL();
// Inicializa la ventana
void initWindow();
// Inicializa el gestor de eventos
void initEvent();
// Inicializa jail_audio
void initJailAudio();
// Inicializa el objeto screen
void initScreen();
// Inicializa el objeto input
void initInput();
// Inicializa el texto
void initText();
// Inicializa el sprite
void initSprite();
// Inicializa todo
void initAll(char *argv[]);
// Comprueba el teclado y los eventos
void checkEvents();
// Comprueba el objeto input
void checkInput();
// Actualiza el sprite
void updateSprite();
// Actualiza el degradado
void updateGradient();
// Actualiza la lógica del programa
void update();
// Dibuja un degradado de fondo
void renderGradient();
// Dibuja el texto
void renderText();
// Dibuja los elementos del programa en pantalla
void render();
// Libera la memoria reservada
void freeAll();
// Inicializa las opciones
void initOptions()
@@ -43,7 +114,7 @@ void initOptions()
}
// Inicializa la lista de recursos
void initAsset()
void initAsset(char *argv[])
{
asset = new Asset(argv[0]);
asset->add("/data/music.ogg", t_music);
@@ -63,18 +134,16 @@ void initAsset()
}
}
int main(int argc, char *argv[])
// Inicializa SDL
void initSDL()
{
// Inicializa las opciones
initOptions();
// Inicializa la lista de recursos
initAsset();
// Inicializa SDL y la ventana
SDL_Init(SDL_INIT_EVERYTHING);
}
// Inicializa la ventana
void initWindow()
{
window = SDL_CreateWindow("Jail Engine DEMO", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, options->screen.nativeWidth * options->screen.nativeZoom * options->screen.windowZoom, options->screen.nativeHeight * options->screen.nativeZoom * options->screen.windowZoom, SDL_WINDOW_SHOWN);
// window = SDL_CreateWindow("jail_engine_demo", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 320, 240, SDL_WINDOW_SHOWN);
if (window != nullptr)
{
renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
@@ -91,30 +160,49 @@ int main(int argc, char *argv[])
{
exit(EXIT_FAILURE);
}
event = new SDL_Event();
}
// Inicializa jail_audio
// Inicializa el gestor de eventos
void initEvent()
{
event = new SDL_Event();
}
// Inicializa jail_audio
void initJailAudio()
{
JA_Init(48000, AUDIO_S16, 2);
JA_Music_t *music;
JA_Sound_t *sound;
music = JA_LoadMusic(asset->get("music.ogg").c_str());
sound = JA_LoadSound(asset->get("sound.wav").c_str());
// JA_PlayMusic(music, true);
}
// Inicializa el objeto screen
Screen *screen = new Screen(window, renderer, options);
// Inicializa el objeto screen
void initScreen()
{
screen = new Screen(window, renderer, options);
screen->addNotifier(asset->get("notify.png"), asset->get("smb2.png"), asset->get("smb2.txt"), asset->get("notify.wav"));
}
// Inicializa el objeto input
Input *input = new Input(asset->get("gamecontrollerdb.txt"), options->console);
string controllerName = input->getNumControllers() > 0 ? input->getControllerName(0) : "No se ha encontrado ningun mando";
// Inicializa el objeto input
void initInput()
{
input = new Input(asset->get("gamecontrollerdb.txt"), options->console);
controllerName = input->getNumControllers() > 0 ? input->getControllerName(0) : "No se ha encontrado ningun mando";
}
// Inicializa el texto
Text *text = new Text(asset->get("smb2.txt"), asset->get("smb2.png"), renderer);
Text *debugText = new Text(asset->get("debug.txt"), asset->get("debug.png"), renderer);
// Inicializa el texto
void initText()
{
text = new Text(asset->get("smb2.txt"), asset->get("smb2.png"), renderer);
debugText = new Text(asset->get("debug.txt"), asset->get("debug.png"), renderer);
}
// Inicializa el sprite
Texture *texture = new Texture(renderer, asset->get("z80.png"));
MovingSprite *sprite = new MovingSprite();
// Inicializa el sprite
void initSprite()
{
texture = new Texture(renderer, asset->get("z80.png"));
sprite = new MovingSprite();
sprite->setRenderer(renderer);
sprite->setTexture(texture);
sprite->setPosX(140);
@@ -124,14 +212,45 @@ int main(int argc, char *argv[])
sprite->setSpriteClip({0, 0, 16, 32});
sprite->setVelX(1);
sprite->setVelY(2);
}
// Bucle principal
// JA_PlayMusic(music, true);
bool should_exit = false;
while (!should_exit)
{
// Inicializa todo
void initAll(char *argv[])
{
// Inicializa las opciones
initOptions();
// Comprueba el teclado y los eventos
// Inicializa la lista de recursos
initAsset(argv);
// Inicializa SDL
initSDL();
// Inicializa la ventana
initWindow();
// Inicializa el gestor de eventos
initEvent();
// Inicializa jail_audio
initJailAudio();
// Inicializa el objeto screen
initScreen();
// Inicializa el objeto input
initInput();
// Inicializa el texto
initText();
// Inicializa el sprite
initSprite();
}
// Comprueba el teclado y los eventos
void checkEvents()
{
while (SDL_PollEvent(event))
{
if (event->type == SDL_QUIT)
@@ -165,7 +284,12 @@ int main(int argc, char *argv[])
}
}
}
string inputPressed = "";
}
// Comprueba el objeto input
void checkInput()
{
inputPressed = "";
if (input->checkInput(INPUT_LEFT))
{
inputPressed = "Izquierda";
@@ -182,20 +306,11 @@ int main(int argc, char *argv[])
{
inputPressed = "Abajo";
}
}
// Actualiza la lógica del programa
if (SDL_GetTicks() - ticks > ticksSpeed)
{
// Actualiza la variable
ticks = SDL_GetTicks();
// Incrementa el contador
counter++;
// Actualiza el objeto screen
screen->update();
// Actualiza el sprite
// Actualiza el sprite
void updateSprite()
{
if (sprite->getPosX() + sprite->getWidth() > options->screen.nativeWidth or sprite->getPosX() < 0)
{
sprite->undoMoveX();
@@ -212,6 +327,7 @@ int main(int argc, char *argv[])
sprite->setVelX(spr_force * spr_direction);
JA_PlaySound(sound);
}
if (sprite->getPosY() + sprite->getHeight() > options->screen.nativeHeight or sprite->getPosY() < 0)
{
sprite->undoMoveY();
@@ -228,9 +344,13 @@ int main(int argc, char *argv[])
sprite->setVelY(spr_force * spr_direction);
JA_PlaySound(sound);
}
sprite->update();
// Actualiza el degradado
sprite->update();
}
// Actualiza el degradado
void updateGradient()
{
if (counter % 1 == 0)
{
gradBreathDirection == 0 ? gradCurrentColor-- : gradCurrentColor++;
@@ -243,13 +363,40 @@ int main(int argc, char *argv[])
gradBreathDirection = 0;
}
}
}
// Actualiza la lógica del programa
void update()
{
// Comprueba el teclado y los eventos
checkEvents();
// Comprueba el objeto input
checkInput();
// Actualiza la lógica del programa
if (SDL_GetTicks() - ticks > ticksSpeed)
{
// Actualiza la variable
ticks = SDL_GetTicks();
// Incrementa el contador
counter++;
// Actualiza el objeto screen
screen->update();
// Actualiza el sprite
updateSprite();
// Actualiza el degradado
updateGradient();
}
}
// Dibuja en pantalla
screen->start();
screen->clean();
// Dibuja un degradado de fondo
// Dibuja un degradado de fondo
void renderGradient()
{
const int gradFirstLine = options->screen.nativeHeight / 3;
const int gradLastLine = options->screen.nativeHeight;
const int gradNumLines = gradLastLine - gradFirstLine;
@@ -270,11 +417,11 @@ int main(int argc, char *argv[])
SDL_SetRenderDrawColor(renderer, 0x00, 0x00, 0xFF, alpha);
SDL_RenderDrawLine(renderer, 0, gradLastLine - i, options->screen.nativeWidth, gradLastLine - i);
}
}
// Dibuja el sprite
sprite->render();
// Escribe el texto
// Dibuja el texto
void renderText()
{
text->setZoom(2);
text->writeDX(TXT_CENTER | TXT_STROKE, options->screen.nativeWidth / 2, text->getCharacterSize(), "Jail Engine DEMO", 1, {255, 255, 255}, 1, {0, 0, 192});
text->disableZoom();
@@ -284,42 +431,50 @@ int main(int argc, char *argv[])
debugText->writeDX(TXT_CENTER | TXT_COLOR | TXT_STROKE, options->screen.nativeWidth / 2, text->getCharacterSize() * 11, controllerName, 1, {240, 240, 240}, 1, {0, 0, 192});
debugText->writeDX(TXT_CENTER | TXT_COLOR | TXT_STROKE, options->screen.nativeWidth / 2, text->getCharacterSize() * 13, inputPressed, 1, {240, 240, 240}, 1, {0, 0, 192});
debugText->writeDX(TXT_CENTER | TXT_COLOR | TXT_STROKE, options->screen.nativeWidth / 2, options->screen.nativeHeight - (text->getCharacterSize() * 2), "Pulsa 'ESCAPE' para terminar el programa", 1, {240, 240, 240}, 1, {0, 0, 192});
}
// Dibuja los elementos del programa en pantalla
void render()
{
// Prepara el objeto screen para dibujar
screen->start();
screen->clean();
// Dibuja un degradado de fondo
renderGradient();
// Dibuja el sprite
sprite->render();
// Dinuja el texto
renderText();
// Vuelca el buffer en pantalla
screen->blit();
}
}
// Libera la memoria reservada
void freeAll()
{
// Finaliza el sprite
if (sprite != nullptr)
{
delete sprite;
}
if (texture != nullptr)
{
delete texture;
}
// Finaliza el texto
if (text != nullptr)
{
delete text;
}
if (debugText != nullptr)
{
delete debugText;
}
// Finaliza el objeto input
if (input != nullptr)
{
delete input;
}
// Finaliza el objeto screen
if (screen != nullptr)
{
delete screen;
}
// Finaliza jail_audio
JA_DeleteSound(sound);
@@ -327,24 +482,37 @@ int main(int argc, char *argv[])
// Finaliza el objeto con la lista de recuros
if (asset != nullptr)
{
delete asset;
}
// Finaliza las opciones
if (options != nullptr)
{
delete options;
}
// Finaliza SDL y la ventana
if (event != nullptr)
{
delete event;
}
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_Quit();
}
int main(int argc, char *argv[])
{
// Inicializa todo
initAll(argv);
// Bucle principal
while (!should_exit)
{
// Actualiza la lógica del programa
update();
// Dibuja los elementos del programa en pantalla
render();
}
// Libera la memoria reservada
freeAll();
return 0;
}