149 lines
3.4 KiB
C++
149 lines
3.4 KiB
C++
/*
|
|
|
|
Ejemplo de uso de las unidades incluídas en jail_engine
|
|
Código fuente creado por JailDesigner
|
|
|
|
*/
|
|
|
|
#include <SDL2/SDL.h>
|
|
#include <iostream>
|
|
#include "units/jail_audio.h"
|
|
#include "units/text.h"
|
|
#include "units/utils.h"
|
|
#include "units/asset.h"
|
|
|
|
SDL_Event *event;
|
|
SDL_Window *window;
|
|
SDL_Renderer *renderer;
|
|
int ticks = 0;
|
|
int ticksSpeed = 15;
|
|
int counter = 0;
|
|
|
|
int main(int argc, char *argv[])
|
|
{
|
|
|
|
// Inicializa las opciones
|
|
struct options_t *options = new options_t;
|
|
options->gameWidth = 640;
|
|
options->gameHeight = 480;
|
|
options->console = true;
|
|
|
|
// Inicializa la lista de recursos
|
|
Asset *asset = new Asset(argv[0]);
|
|
asset->add("/data/music.ogg", t_music);
|
|
asset->add("/data/sound.wav", t_sound);
|
|
asset->add("/data/smb2.txt", t_font);
|
|
asset->add("/data/smb2.png", t_bitmap);
|
|
asset->setVerbose(options->console);
|
|
if (!asset->check())
|
|
{
|
|
exit(EXIT_FAILURE);
|
|
}
|
|
|
|
// Inicializa SDL y la ventana
|
|
SDL_Init(SDL_INIT_EVERYTHING);
|
|
window = SDL_CreateWindow("jail_engine_demo", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, options->gameWidth, options->gameHeight, SDL_WINDOW_SHOWN);
|
|
if (window != nullptr)
|
|
{
|
|
renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
|
|
if (renderer == nullptr)
|
|
{
|
|
exit(EXIT_FAILURE);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
exit(EXIT_FAILURE);
|
|
}
|
|
event = new SDL_Event();
|
|
|
|
// Inicializa jail_audio
|
|
JA_Init(48000, AUDIO_S16, 2);
|
|
|
|
JA_Music_t *music;
|
|
JA_Sound_t *peiv;
|
|
|
|
music = JA_LoadMusic(asset->get("music.ogg").c_str());
|
|
peiv = JA_LoadSound(asset->get("sound.wav").c_str());
|
|
int volume = 128;
|
|
|
|
// Inicializa el texto
|
|
Text *text = new Text(asset->get("smb2.txt"), asset->get("smb2.png"), renderer);
|
|
|
|
// Bucle principal
|
|
// JA_PlayMusic(music, true);
|
|
bool should_exit = false;
|
|
while (!should_exit)
|
|
{
|
|
|
|
// Comprueba el teclado y los eventos
|
|
while (SDL_PollEvent(event))
|
|
{
|
|
if (event->type == SDL_QUIT)
|
|
{
|
|
should_exit = true;
|
|
break;
|
|
}
|
|
if (event->type == SDL_KEYDOWN)
|
|
{
|
|
switch (event->key.keysym.scancode)
|
|
{
|
|
|
|
default:
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
// Actualiza la lógica del programa
|
|
if (SDL_GetTicks() - ticks > ticksSpeed)
|
|
{
|
|
// Actualiza la variable
|
|
ticks = SDL_GetTicks();
|
|
|
|
// Incrementa el contador
|
|
counter++;
|
|
}
|
|
|
|
// Dibuja en pantalla
|
|
SDL_SetRenderDrawColor(renderer, 0x00, 0x00, 0x00, 0xFF);
|
|
SDL_RenderClear(renderer);
|
|
// Dibuja un degradado de fondo
|
|
const int gradFirstLine = options->gameHeight / 3;
|
|
const int gradLastLine = options->gameHeight;
|
|
const int gradNumLines = gradLastLine - gradFirstLine;
|
|
const int gradColorFrom = 0;
|
|
const int gradColorTo = 192;
|
|
|
|
for (int i = gradFirstLine; i < gradLastLine; ++i)
|
|
{
|
|
float step = ((float)(i - gradFirstLine) / gradNumLines);
|
|
int color = gradColorFrom + ((gradColorTo - gradColorFrom) * step);
|
|
SDL_SetRenderDrawColor(renderer, color, 0x00, 0x00, 0xFF);
|
|
SDL_RenderDrawLine(renderer, 0, i, options->gameWidth, i);
|
|
}
|
|
// Escribe el texto
|
|
text->writeCentered(options->gameWidth / 2, text->getCharacterSize(), "Jail Engine DEMO");
|
|
// Vuelca el buffer en pantalla
|
|
SDL_RenderPresent(renderer);
|
|
}
|
|
|
|
// Finaliza el objeto con la lista de recuros
|
|
delete asset;
|
|
|
|
// Finaliza el texto
|
|
delete text;
|
|
|
|
// Finaliza jail_audio
|
|
JA_DeleteSound(peiv);
|
|
JA_DeleteMusic(music);
|
|
|
|
// Finaliza SDL y la ventana
|
|
SDL_DestroyRenderer(renderer);
|
|
SDL_DestroyWindow(window);
|
|
delete event;
|
|
SDL_Quit();
|
|
|
|
return 0;
|
|
}
|