Compare commits
23 Commits
v1.08
...
2ac425483b
| Author | SHA1 | Date | |
|---|---|---|---|
| 2ac425483b | |||
| fc01676df2 | |||
| e361d295c1 | |||
| f6098a479b | |||
| 7a0bc5c9ae | |||
| 5f68c6256f | |||
| debcc3409e | |||
| c86a6496b3 | |||
| 6bb877b510 | |||
| ec73c5fa30 | |||
| 4dd6c94730 | |||
| e1d6aff724 | |||
| bcb2e96069 | |||
| e23f6b5ed9 | |||
| 9cb57e2ff2 | |||
| cc0f050c50 | |||
| d75a733985 | |||
| 33d91dab55 | |||
| bd6807d655 | |||
| c6d15bb96f | |||
| e20cc3b4bb | |||
| 0baf7e5e2c | |||
| d9d4818d8a |
4
.gitignore
vendored
4
.gitignore
vendored
@@ -13,4 +13,6 @@ thumbs.db
|
||||
*_debug*
|
||||
sync_jail_engine.sh
|
||||
jaildoctors_dilemma*
|
||||
todo
|
||||
todo
|
||||
build/
|
||||
linux_utils/
|
||||
85
CMakeLists.txt
Normal file
85
CMakeLists.txt
Normal file
@@ -0,0 +1,85 @@
|
||||
# CMakeLists.txt
|
||||
|
||||
cmake_minimum_required(VERSION 3.10)
|
||||
project(jaildoctors_dilemma VERSION 1.00)
|
||||
|
||||
# Configuración de compilador para MinGW en Windows, si es necesario
|
||||
if(WIN32 AND NOT CMAKE_CXX_COMPILER_ID MATCHES "MSVC")
|
||||
set(CMAKE_CXX_COMPILER "g++")
|
||||
set(CMAKE_C_COMPILER "gcc")
|
||||
endif()
|
||||
|
||||
# Establecer estándar de C++
|
||||
set(CMAKE_CXX_STANDARD 20)
|
||||
set(CMAKE_CXX_STANDARD_REQUIRED True)
|
||||
|
||||
# Configuración global de flags de compilación
|
||||
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall")
|
||||
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -Os -ffunction-sections -fdata-sections")
|
||||
|
||||
# Define el directorio de los archivos fuente
|
||||
set(DIR_SOURCES "${CMAKE_SOURCE_DIR}/source")
|
||||
|
||||
# Cargar todos los archivos fuente en DIR_SOURCES
|
||||
file(GLOB SOURCES "${DIR_SOURCES}/*.cpp")
|
||||
|
||||
# Verificar si se encontraron archivos fuente
|
||||
if(NOT SOURCES)
|
||||
message(FATAL_ERROR "No se encontraron archivos fuente en ${DIR_SOURCES}. Verifica que el directorio existe y contiene archivos .cpp.")
|
||||
endif()
|
||||
|
||||
# Configuración de SDL2
|
||||
find_package(SDL2 REQUIRED)
|
||||
if(SDL2_FOUND)
|
||||
message(STATUS "SDL2 encontrado: ${SDL2_INCLUDE_DIRS}")
|
||||
include_directories(${SDL2_INCLUDE_DIRS})
|
||||
link_directories(${SDL2_LIBDIR})
|
||||
else()
|
||||
message(FATAL_ERROR "SDL2 no encontrado")
|
||||
endif()
|
||||
|
||||
# Incluye rutas de SDL2 obtenidas con pkg-config
|
||||
include_directories(/usr/local/include /usr/local/include/SDL2)
|
||||
link_directories(/usr/local/lib)
|
||||
|
||||
# Definir las bibliotecas comunes
|
||||
set(LIBS SDL2)
|
||||
|
||||
# Configuración común de salida de ejecutables en el directorio raíz
|
||||
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR})
|
||||
|
||||
# Añadir ejecutable principal
|
||||
add_executable(${PROJECT_NAME} ${SOURCES})
|
||||
|
||||
# Añadir definiciones de compilación dependiendo del tipo de build
|
||||
target_compile_definitions(${PROJECT_NAME} PRIVATE $<$<CONFIG:DEBUG>:DEBUG VERBOSE>)
|
||||
|
||||
# Enlazar bibliotecas
|
||||
target_link_libraries(${PROJECT_NAME} ${LIBS})
|
||||
|
||||
# Configuración específica para cada plataforma
|
||||
if(WIN32)
|
||||
target_compile_definitions(${PROJECT_NAME} PRIVATE WINDOWS_BUILD)
|
||||
target_link_libraries(${PROJECT_NAME} mingw32 opengl32 gdi32 winmm imm32 ole32 version)
|
||||
elseif(APPLE)
|
||||
set(LIBS ${LIBS} "-framework OpenGL")
|
||||
target_compile_definitions(${PROJECT_NAME} PRIVATE MACOS_BUILD)
|
||||
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-deprecated")
|
||||
# Configurar compilación para Apple Silicon
|
||||
set(CMAKE_OSX_ARCHITECTURES "arm64")
|
||||
elseif(UNIX AND NOT APPLE)
|
||||
set(LIBS ${LIBS} GL)
|
||||
target_compile_definitions(${PROJECT_NAME} PRIVATE LINUX_BUILD)
|
||||
target_link_libraries(${PROJECT_NAME} ${LIBS})
|
||||
endif()
|
||||
|
||||
# Añadir OpenGL a las bibliotecas enlazadas
|
||||
if(NOT WIN32)
|
||||
find_package(OpenGL REQUIRED)
|
||||
if(OPENGL_FOUND)
|
||||
message(STATUS "OpenGL encontrado: ${OPENGL_LIBRARIES}")
|
||||
target_link_libraries(${PROJECT_NAME} ${OPENGL_LIBRARIES})
|
||||
else()
|
||||
message(FATAL_ERROR "OpenGL no encontrado")
|
||||
endif()
|
||||
endif()
|
||||
27
Makefile
27
Makefile
@@ -1,8 +1,8 @@
|
||||
executable = jaildoctors_dilemma
|
||||
source = source/*.cpp source/jail_engine/*.cpp
|
||||
source = source/*.cpp
|
||||
appName = JailDoctor's Dilemma
|
||||
releaseFolder = jdd_release
|
||||
version = v1.08
|
||||
version = v1.09
|
||||
|
||||
# Release names
|
||||
windowsRelease = $(executable)-$(version)-win32-x64.zip
|
||||
@@ -10,14 +10,17 @@ macosIntelRelease = $(executable)-$(version)-macos-intel.dmg
|
||||
macosAppleSiliconRelease = $(executable)-$(version)-macos-apple-silicon.dmg
|
||||
linuxRelease = $(executable)-$(version)-linux.tar.gz
|
||||
|
||||
# Specify the C++ standard
|
||||
cpp_standard = c++20
|
||||
|
||||
windows:
|
||||
@echo off
|
||||
g++ $(source) -std=c++11 -Wall -Os -lmingw32 -lws2_32 -lSDL2main -lSDL2 -ffunction-sections -fdata-sections -Wl,--gc-sections -static-libstdc++ -Wl,-subsystem,windows -o "$(executable).exe"
|
||||
g++ $(source) -std=$(cpp_standard) -Wall -Os -lmingw32 -lws2_32 -lSDL2main -lSDL2 -lopengl32 -ffunction-sections -fdata-sections -Wl,--gc-sections -static-libstdc++ -Wl,-subsystem,windows -o "$(executable).exe"
|
||||
strip -s -R .comment -R .gnu.version "$(executable).exe" --strip-unneeded
|
||||
|
||||
windows_debug:
|
||||
@echo off
|
||||
g++ $(source) -D DEBUG -std=c++11 -Wall -Os -lmingw32 -lws2_32 -lSDL2main -lSDL2 -ffunction-sections -fdata-sections -Wl,--gc-sections -static-libstdc++ -Wl,-subsystem,windows -o "$(executable)_debug.exe"
|
||||
g++ $(source) -D DEBUG -std=$(cpp_standard) -Wall -Os -lmingw32 -lws2_32 -lSDL2main -lSDL2 -lopengl32 -ffunction-sections -fdata-sections -Wl,--gc-sections -static-libstdc++ -Wl,-subsystem,windows -o "$(executable)_debug.exe"
|
||||
strip -s -R .comment -R .gnu.version "$(executable)_debug.exe" --strip-unneeded
|
||||
|
||||
windows_release:
|
||||
@@ -38,7 +41,7 @@ windows_release:
|
||||
powershell Copy-Item "release\*.dll" -Destination "$(releaseFolder)"
|
||||
|
||||
# Build
|
||||
g++ $(source) -std=c++11 -Wall -Os -lmingw32 -lws2_32 -lSDL2main -lSDL2 -ffunction-sections -fdata-sections -Wl,--gc-sections -static-libstdc++ -Wl,-subsystem,windows -o "$(releaseFolder)/$(executable).exe"
|
||||
g++ $(source) -std=$(cpp_standard) -Wall -Os -lmingw32 -lws2_32 -lSDL2main -lSDL2 -lopengl32 -ffunction-sections -fdata-sections -Wl,--gc-sections -static-libstdc++ -Wl,-subsystem,windows -o "$(releaseFolder)/$(executable).exe"
|
||||
strip -s -R .comment -R .gnu.version "$(releaseFolder)/$(executable).exe" --strip-unneeded
|
||||
|
||||
# Create ZIP
|
||||
@@ -49,10 +52,10 @@ windows_release:
|
||||
powershell if (Test-Path "$(releaseFolder)") {Remove-Item "$(releaseFolder)" -Recurse -Force}
|
||||
|
||||
macos:
|
||||
clang++ $(source) -std=c++11 -Wall -Os -lSDL2 -ffunction-sections -fdata-sections -o "$(executable)"
|
||||
clang++ $(source) -std=$(cpp_standard) -Wall -Os -lSDL2 -framework OpenGL -Wno-deprecated -ffunction-sections -fdata-sections -o "$(executable)"
|
||||
|
||||
macos_debug:
|
||||
clang++ $(source) -D DEBUG -std=c++11 -Wall -Os -lSDL2 -ffunction-sections -fdata-sections -o "$(executable)_debug"
|
||||
clang++ $(source) -D DEBUG -std=$(cpp_standard) -Wall -Os -lSDL2 -framework OpenGL -Wno-deprecated -ffunction-sections -fdata-sections -o "$(executable)_debug"
|
||||
|
||||
macos_release:
|
||||
# Remove data and possible data from previous builds
|
||||
@@ -87,7 +90,7 @@ macos_release:
|
||||
ln -s /Applications "$(releaseFolder)"/Applications
|
||||
|
||||
# Build INTEL
|
||||
clang++ $(source) -D MACOS_BUNDLE -std=c++11 -Wall -Os -framework SDL2 -F ./Frameworks -ffunction-sections -fdata-sections -o "$(releaseFolder)/$(appName).app/Contents/MacOS/$(executable)" -rpath @executable_path/../Frameworks/ -target x86_64-apple-macos10.12
|
||||
clang++ $(source) -D MACOS_BUNDLE -std=$(cpp_standard) -Wall -Os -framework SDL2 -F ./Frameworks -framework OpenGL -Wno-deprecated -ffunction-sections -fdata-sections -o "$(releaseFolder)/$(appName).app/Contents/MacOS/$(executable)" -rpath @executable_path/../Frameworks/ -target x86_64-apple-macos10.12
|
||||
|
||||
# Build INTEL DMG
|
||||
hdiutil create tmp.dmg -ov -volname "$(appName)" -fs HFS+ -srcfolder "$(releaseFolder)"
|
||||
@@ -95,7 +98,7 @@ macos_release:
|
||||
rm -f tmp.dmg
|
||||
|
||||
# Build APPLE SILICON
|
||||
clang++ $(source) -D MACOS_BUNDLE -std=c++11 -Wall -Os -framework SDL2 -F ./Frameworks -ffunction-sections -fdata-sections -o "$(releaseFolder)/$(appName).app/Contents/MacOS/$(executable)" -rpath @executable_path/../Frameworks/ -target arm64-apple-macos11
|
||||
clang++ $(source) -D MACOS_BUNDLE -std=$(cpp_standard) -Wall -Os -framework SDL2 -F ./Frameworks -framework OpenGL -Wno-deprecated -ffunction-sections -fdata-sections -o "$(releaseFolder)/$(appName).app/Contents/MacOS/$(executable)" -rpath @executable_path/../Frameworks/ -target arm64-apple-macos11
|
||||
|
||||
# Build APPLE SILICON DMG
|
||||
hdiutil create tmp.dmg -ov -volname "$(appName)" -fs HFS+ -srcfolder "$(releaseFolder)"
|
||||
@@ -107,11 +110,11 @@ macos_release:
|
||||
rm -rdf "$(releaseFolder)"
|
||||
|
||||
linux:
|
||||
g++ $(source) -std=c++11 -Wall -Os -lSDL2 -ffunction-sections -fdata-sections -Wl,--gc-sections -o "$(executable)"
|
||||
g++ $(source) -std=$(cpp_standard) -Wall -Os -lSDL2 -lGL -ffunction-sections -fdata-sections -Wl,--gc-sections -o "$(executable)"
|
||||
strip -s -R .comment -R .gnu.version "$(executable)" --strip-unneeded
|
||||
|
||||
linux_debug:
|
||||
g++ $(source) -D DEBUG -std=c++11 -Wall -Os -lSDL2 -ffunction-sections -fdata-sections -Wl,--gc-sections -o "$(executable)_debug"
|
||||
g++ $(source) -D DEBUG -std=$(cpp_standard) -Wall -Os -lSDL2 -lGL -ffunction-sections -fdata-sections -Wl,--gc-sections -o "$(executable)_debug"
|
||||
strip -s -R .comment -R .gnu.version "$(executable)_debug" --strip-unneeded
|
||||
|
||||
linux_release:
|
||||
@@ -131,7 +134,7 @@ linux_release:
|
||||
rm -f "$(releaseFolder)/data/room/standard.tsx"
|
||||
|
||||
# Build
|
||||
g++ $(source) -std=c++11 -Wall -Os -lSDL2 -ffunction-sections -fdata-sections -Wl,--gc-sections -o "$(releaseFolder)/$(executable)"
|
||||
g++ $(source) -std=$(cpp_standard) -Wall -Os -lSDL2 -lGL -ffunction-sections -fdata-sections -Wl,--gc-sections -o "$(releaseFolder)/$(executable)"
|
||||
strip -s -R .comment -R .gnu.version "$(releaseFolder)/$(executable)" --strip-unneeded
|
||||
|
||||
# Pack files
|
||||
|
||||
100
README.md
100
README.md
@@ -1,89 +1,65 @@
|
||||
# JailDoctor's Dilemma
|
||||
|
||||
JailDoc es un Jailer. A los Jailers les gusta empezar proyectos. A nadie le gusta terminarlos. Los Jailers viven en la Jail. A la Jail va uno a empezar proyectos. A la Jail va uno a enseñar sus proyectos. A la Jail va uno a aprender como empezar nuevos proyectos. A la Jail va uno a ayudar a sus compañeros a que empiecen nuevos proyectos.
|
||||
JailDoc és un Jailer. Als Jailers els agrada començar projectes. A ningú li agrada acabar-los. Els Jailers viuen a la Jail. A la Jail s'hi va a començar projectes. A la Jail s'hi va a ensenyar els projectes. A la Jail s'hi va a aprendre com començar nous projectes. A la Jail s'hi va a ajudar els companys a començar nous projectes.
|
||||
|
||||
|
||||

|
||||
|
||||

|
||||
JailDoc és un Jailer destacat entre els Jailers. Té més projectes començats que ningú i és qui més ajuda als altres a iniciar els seus.
|
||||
|
||||
|
||||
Però un dia, va passar una cosa inesperada. Algú va acabar un projecte. Algú va alliberar el *Puzzle Jail Facker*. Un autèntic desaprensiu.
|
||||
|
||||
JailDoc es un Jailer destacado entre los Jailers. Tiene más proyectos empezados que nadie y es el que más ayuda a que los demas empiecen los suyos.
|
||||
Això va fer que JailDoc prenguera una decisió: acabaria i lliuraria un dels seus projectes. Però, quin? *JailBattle*? *Sigmasuá*? *Calculín Doom*? Quin dilema! Finalment, es va arromangar i va decidir acabar i lliurar **tots** els seus projectes inacabats. Ho aconseguirà?
|
||||
|
||||
|
||||
---
|
||||
|
||||
Un día, ocurrió algo. Alguien terminó un proyecto. Alguien liberó el *Puzzle Jail Facker*. Algún desaprensivo.
|
||||
## Jugabilitat
|
||||
|
||||
|
||||
Ajuda a JailDoc a recuperar les peces dels seus projectes, que estan escampades per qualsevol racó de l'Univers Jailer. Hi ha més de **150 peces** repartides en **60 pantalles**. Algunes són senzilles, però en altres hauràs de calcular molt bé els teus moviments si no vols acabar com un *arounder* més.
|
||||
|
||||
Esto hizo que JailDoc decidiera terminar y entregar uno de sus proyectos, pero, ¿cual? ¿JailBattle? ¿Sigmasuá? ¿Calculín Doom? Menudo dilema. JailDoc se arremangó y decidió finalizar y entregar todos sus proyectos inacabados. ¿Lo logrará?
|
||||

|
||||
|
||||
|
||||
Quan hages recuperat la major part de les peces, dirigeix-te a la Jail per mostrar als Jailers com es finalitza un projecte. Però compte! Bry no et deixarà entrar així com així. Només aquells que han creat un *Fire Effect* o un *Facedor de Tornejos* són dignes d'aquesta fita.
|
||||
|
||||
## Jugabilidad
|
||||
---
|
||||
|
||||
Ayuda a JailDoc a recuperar las partes de su proyecto que estan desperdigadas por cualquier lugar del Universo Jailer. Hay mas de **150 piezas** desperdigadas por **60 pantallas**. Algunas son un paseo, pero en otras tendras que calcular muy bien tus movimientos si no quieres acabar como un arounder del montón.
|
||||
## Controls
|
||||
|
||||
|
||||
El joc permet tant l'ús del teclat com d'un comandament. Les tecles per a jugar són les següents:
|
||||
|
||||

|
||||
- **Cursors**: Per moure's a l'esquerra o dreta i per saltar. Es poden modificar les tecles en el fitxer de configuració, triant entre aquestes opcions:
|
||||
- O, P per moure's i Q per saltar.
|
||||
- A, D per moure's i W per saltar.
|
||||
|
||||
|
||||
- **Tecla M**: Activa o desactiva la música.
|
||||
- **Tecla P**: Pausa el joc.
|
||||
- **Tecla ESC**: Ix del joc si estàs jugant. Tanca el programa en qualsevol altra circumstància.
|
||||
- **Tecla F1**: Disminueix la mida de la finestra.
|
||||
- **Tecla F2**: Augmenta la mida de la finestra.
|
||||
- **Tecla F3**: Alterna entre el mode de pantalla completa i el mode finestra.
|
||||
- **Tecla F4**: Activa o desactiva els shaders
|
||||
- **Tecla F5**: Canvia la paleta de colors del joc.
|
||||
- **Tecla B**: Activa o desactiva el marge de colors en mode finestra.
|
||||
|
||||
Cuando consigas recuperar gran parte de las piezas desperdigadas, dirigete a la Jail a mostrar a los Jailers como se termina un proyecto. Ten en cuenta que Bry no te dejará entrar. Solo aquellos que han realizado un *Fire Effect* o un *Facedor de Tornejos* son dignos de tal privilegio.
|
||||

|
||||
|
||||
|
||||
---
|
||||
|
||||
## Controles
|
||||
## Dades del programa
|
||||
|
||||
El juego permite tanto el uso del teclado como de un mando de control. Las teclas para manejar el juego son las siguientes:
|
||||
El programa guarda automàticament la configuració del mode de vídeo i les estadístiques del joc a la teua carpeta personal del sistema. La ubicació d'aquesta carpeta depén del sistema operatiu que utilitzes:
|
||||
|
||||
|
||||
- **Windows**: `C:\Users\<nom_d'usuari>\AppData\Roaming\jailgames\jaildoctors_dilemma`
|
||||
- **MacOS**: `~/Library/Application Support/jailgames/jaildoctors_dilemma`
|
||||
- **Linux**: `~/.jailgames/jaildoctors_dilemma`
|
||||
|
||||
- **Cursores**: Para mover a izquierda o derecha a JailDoc y para saltar. En el fichero de configuración se pueden cambiar las teclas por otras opciones prefijadas: O, P para moverse y Q para saltar o A, D para moverse y W para saltar.
|
||||
Dins de la carpeta es troba el fitxer de configuració `config.txt`, on es pot modificar la configuració per connectar-se al servei en línia, i els fitxers `stats.csv` i `stats_buffer.csv`, que contenen informació sobre les estadístiques del joc.
|
||||
|
||||
- **Tecla M**: Activa o desactiva la música
|
||||
---
|
||||
|
||||
- **Tecla P**: Pone en pausa el juego
|
||||
## Agraïments
|
||||
|
||||
- **Tecla ESC**: Sale del juego si estas jugando. Sale del programa en cualquier otra circunstancia
|
||||
Gràcies, com sempre, a tots els Jailers per motivar-me a crear aquest joc i per ajudar-me en els moments de dubte en escriure el codi. I, com sempre, un agraïment especial a JailDoc per la seua unitat de *Jail_Audio* i per qualsevol altre codi, ajuda o ensenyament que haja necessitat per a completar el programa.
|
||||
|
||||
- **Tecla F1**: Disminuye el tamaño de la ventana
|
||||
Si no he perdut el compte, aquest és el quart joc que aconseguisc crear.
|
||||
|
||||
- **Tecla F2**: Aumenta el tamaño de la ventana
|
||||
|
||||
- **Tecla F3**: Cambia entre el modo de pantalla completa y el de ventana
|
||||
|
||||
- **Tecla F5**: Cambia la paleta de colores del juego
|
||||
|
||||
- **Tecla B**: Activa o desactiva el borde de colores de la pantalla cuando el programa se ejecuta en modo de ventana
|
||||
|
||||
|
||||
|
||||

|
||||
|
||||
## Datos del programa
|
||||
El programa guarda automáticamente la configuración del modo de video y las estadísticas de juego en tu carpeta personal del sistema. Esta carpeta tiene una ubicación distinta en función del sistema operativo que utilices.
|
||||
|
||||
En **Windows** se encuentra en:
|
||||
`C:\Users\<nombre_de_usuario>\AppData\Roaming\jailgames\jaildoctors_dilemma`
|
||||
|
||||
En **MacOS** se encuentra en:
|
||||
`~/Library/Application Support/jailgames/jaildoctors_dilemma`
|
||||
|
||||
En **Linux** se encuentra en:
|
||||
`~/./jailgames/jaildoctors_dilemma`
|
||||
|
||||
En la carpeta está el fichero de configuración `config.txt` donde se puede modificar la configuración para conectarse al servicio online y los ficheros `stats.csv` y `stats_buffer.csv` con información de las estadisticas de juego.
|
||||
|
||||
## Agradecimientos
|
||||
|
||||
Agradecimientos como siempre a todos los Jailers por motivarme a hacer el juego y ayudarme en los momentos de duda a la hora de escribir el código. Y, como siempre, en especial a JailDoc por su unidad de Jail_Audio y cualquier otro código/ayuda/enseñanzas que haya necesitado para terminar el programa.
|
||||
|
||||
|
||||
|
||||
Si no me he descontado, este es el cuarto juego que consigo crear.
|
||||
|
||||
|
||||
|
||||
*13 de noviembre de 2022, JailDesigner*
|
||||
*13 de novembre de 2022, JailDesigner*
|
||||
|
||||
234
data/shaders/crtpi_192.glsl
Normal file
234
data/shaders/crtpi_192.glsl
Normal file
@@ -0,0 +1,234 @@
|
||||
/*
|
||||
crt-pi - A Raspberry Pi friendly CRT shader.
|
||||
|
||||
Copyright (C) 2015-2016 davej
|
||||
|
||||
This program is free software; you can redistribute it and/or modify it
|
||||
under the terms of the GNU General Public License as published by the Free
|
||||
Software Foundation; either version 2 of the License, or (at your option)
|
||||
any later version.
|
||||
|
||||
|
||||
Notes:
|
||||
|
||||
This shader is designed to work well on Raspberry Pi GPUs (i.e. 1080P @ 60Hz on a game with a 4:3 aspect ratio). It pushes the Pi's GPU hard and enabling some features will slow it down so that it is no longer able to match 1080P @ 60Hz. You will need to overclock your Pi to the fastest setting in raspi-config to get the best results from this shader: 'Pi2' for Pi2 and 'Turbo' for original Pi and Pi Zero. Note: Pi2s are slower at running the shader than other Pis, this seems to be down to Pi2s lower maximum memory speed. Pi2s don't quite manage 1080P @ 60Hz - they drop about 1 in 1000 frames. You probably won't notice this, but if you do, try enabling FAKE_GAMMA.
|
||||
|
||||
SCANLINES enables scanlines. You'll almost certainly want to use it with MULTISAMPLE to reduce moire effects. SCANLINE_WEIGHT defines how wide scanlines are (it is an inverse value so a higher number = thinner lines). SCANLINE_GAP_BRIGHTNESS defines how dark the gaps between the scan lines are. Darker gaps between scan lines make moire effects more likely.
|
||||
|
||||
GAMMA enables gamma correction using the values in INPUT_GAMMA and OUTPUT_GAMMA. FAKE_GAMMA causes it to ignore the values in INPUT_GAMMA and OUTPUT_GAMMA and approximate gamma correction in a way which is faster than true gamma whilst still looking better than having none. You must have GAMMA defined to enable FAKE_GAMMA.
|
||||
|
||||
CURVATURE distorts the screen by CURVATURE_X and CURVATURE_Y. Curvature slows things down a lot.
|
||||
|
||||
By default the shader uses linear blending horizontally. If you find this too blury, enable SHARPER.
|
||||
|
||||
BLOOM_FACTOR controls the increase in width for bright scanlines.
|
||||
|
||||
MASK_TYPE defines what, if any, shadow mask to use. MASK_BRIGHTNESS defines how much the mask type darkens the screen.
|
||||
|
||||
*/
|
||||
|
||||
#pragma parameter CURVATURE_X "Screen curvature - horizontal" 0.10 0.0 1.0 0.01
|
||||
#pragma parameter CURVATURE_Y "Screen curvature - vertical" 0.15 0.0 1.0 0.01
|
||||
#pragma parameter MASK_BRIGHTNESS "Mask brightness" 0.70 0.0 1.0 0.01
|
||||
#pragma parameter SCANLINE_WEIGHT "Scanline weight" 6.0 0.0 15.0 0.1
|
||||
#pragma parameter SCANLINE_GAP_BRIGHTNESS "Scanline gap brightness" 0.12 0.0 1.0 0.01
|
||||
#pragma parameter BLOOM_FACTOR "Bloom factor" 1.5 0.0 5.0 0.01
|
||||
#pragma parameter INPUT_GAMMA "Input gamma" 2.4 0.0 5.0 0.01
|
||||
#pragma parameter OUTPUT_GAMMA "Output gamma" 2.2 0.0 5.0 0.01
|
||||
|
||||
// Haven't put these as parameters as it would slow the code down.
|
||||
#define SCANLINES
|
||||
#define MULTISAMPLE
|
||||
#define GAMMA
|
||||
//#define FAKE_GAMMA
|
||||
//#define CURVATURE
|
||||
//#define SHARPER
|
||||
// MASK_TYPE: 0 = none, 1 = green/magenta, 2 = trinitron(ish)
|
||||
#define MASK_TYPE 2
|
||||
|
||||
|
||||
#ifdef GL_ES
|
||||
#define COMPAT_PRECISION mediump
|
||||
precision mediump float;
|
||||
#else
|
||||
#define COMPAT_PRECISION
|
||||
#endif
|
||||
|
||||
#ifdef PARAMETER_UNIFORM
|
||||
uniform COMPAT_PRECISION float CURVATURE_X;
|
||||
uniform COMPAT_PRECISION float CURVATURE_Y;
|
||||
uniform COMPAT_PRECISION float MASK_BRIGHTNESS;
|
||||
uniform COMPAT_PRECISION float SCANLINE_WEIGHT;
|
||||
uniform COMPAT_PRECISION float SCANLINE_GAP_BRIGHTNESS;
|
||||
uniform COMPAT_PRECISION float BLOOM_FACTOR;
|
||||
uniform COMPAT_PRECISION float INPUT_GAMMA;
|
||||
uniform COMPAT_PRECISION float OUTPUT_GAMMA;
|
||||
#else
|
||||
#define CURVATURE_X 0.05
|
||||
#define CURVATURE_Y 0.1
|
||||
#define MASK_BRIGHTNESS 0.80
|
||||
#define SCANLINE_WEIGHT 6.0
|
||||
#define SCANLINE_GAP_BRIGHTNESS 0.12
|
||||
#define BLOOM_FACTOR 3.5
|
||||
#define INPUT_GAMMA 2.4
|
||||
#define OUTPUT_GAMMA 2.2
|
||||
#endif
|
||||
|
||||
/* COMPATIBILITY
|
||||
- GLSL compilers
|
||||
*/
|
||||
|
||||
//uniform vec2 TextureSize;
|
||||
#if defined(CURVATURE)
|
||||
varying vec2 screenScale;
|
||||
#endif
|
||||
varying vec2 TEX0;
|
||||
varying float filterWidth;
|
||||
|
||||
#if defined(VERTEX)
|
||||
//uniform mat4 MVPMatrix;
|
||||
//attribute vec4 VertexCoord;
|
||||
//attribute vec2 TexCoord;
|
||||
//uniform vec2 InputSize;
|
||||
//uniform vec2 OutputSize;
|
||||
|
||||
void main()
|
||||
{
|
||||
#if defined(CURVATURE)
|
||||
screenScale = vec2(1.0, 1.0); //TextureSize / InputSize;
|
||||
#endif
|
||||
filterWidth = (768.0 / 192.0) / 3.0;
|
||||
TEX0 = vec2(gl_MultiTexCoord0.x, 1.0-gl_MultiTexCoord0.y)*1.0001;
|
||||
gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
|
||||
}
|
||||
#elif defined(FRAGMENT)
|
||||
|
||||
uniform sampler2D Texture;
|
||||
|
||||
#if defined(CURVATURE)
|
||||
vec2 Distort(vec2 coord)
|
||||
{
|
||||
vec2 CURVATURE_DISTORTION = vec2(CURVATURE_X, CURVATURE_Y);
|
||||
// Barrel distortion shrinks the display area a bit, this will allow us to counteract that.
|
||||
vec2 barrelScale = 1.0 - (0.23 * CURVATURE_DISTORTION);
|
||||
coord *= screenScale;
|
||||
coord -= vec2(0.5);
|
||||
float rsq = coord.x * coord.x + coord.y * coord.y;
|
||||
coord += coord * (CURVATURE_DISTORTION * rsq);
|
||||
coord *= barrelScale;
|
||||
if (abs(coord.x) >= 0.5 || abs(coord.y) >= 0.5)
|
||||
coord = vec2(-1.0); // If out of bounds, return an invalid value.
|
||||
else
|
||||
{
|
||||
coord += vec2(0.5);
|
||||
coord /= screenScale;
|
||||
}
|
||||
|
||||
return coord;
|
||||
}
|
||||
#endif
|
||||
|
||||
float CalcScanLineWeight(float dist)
|
||||
{
|
||||
return max(1.0-dist*dist*SCANLINE_WEIGHT, SCANLINE_GAP_BRIGHTNESS);
|
||||
}
|
||||
|
||||
float CalcScanLine(float dy)
|
||||
{
|
||||
float scanLineWeight = CalcScanLineWeight(dy);
|
||||
#if defined(MULTISAMPLE)
|
||||
scanLineWeight += CalcScanLineWeight(dy-filterWidth);
|
||||
scanLineWeight += CalcScanLineWeight(dy+filterWidth);
|
||||
scanLineWeight *= 0.3333333;
|
||||
#endif
|
||||
return scanLineWeight;
|
||||
}
|
||||
|
||||
void main()
|
||||
{
|
||||
vec2 TextureSize = vec2(256.0, 192.0);
|
||||
#if defined(CURVATURE)
|
||||
vec2 texcoord = Distort(TEX0);
|
||||
if (texcoord.x < 0.0)
|
||||
gl_FragColor = vec4(0.0);
|
||||
else
|
||||
#else
|
||||
vec2 texcoord = TEX0;
|
||||
#endif
|
||||
{
|
||||
vec2 texcoordInPixels = texcoord * TextureSize;
|
||||
#if defined(SHARPER)
|
||||
vec2 tempCoord = floor(texcoordInPixels) + 0.5;
|
||||
vec2 coord = tempCoord / TextureSize;
|
||||
vec2 deltas = texcoordInPixels - tempCoord;
|
||||
float scanLineWeight = CalcScanLine(deltas.y);
|
||||
vec2 signs = sign(deltas);
|
||||
deltas.x *= 2.0;
|
||||
deltas = deltas * deltas;
|
||||
deltas.y = deltas.y * deltas.y;
|
||||
deltas.x *= 0.5;
|
||||
deltas.y *= 8.0;
|
||||
deltas /= TextureSize;
|
||||
deltas *= signs;
|
||||
vec2 tc = coord + deltas;
|
||||
#else
|
||||
float tempY = floor(texcoordInPixels.y) + 0.5;
|
||||
float yCoord = tempY / TextureSize.y;
|
||||
float dy = texcoordInPixels.y - tempY;
|
||||
float scanLineWeight = CalcScanLine(dy);
|
||||
float signY = sign(dy);
|
||||
dy = dy * dy;
|
||||
dy = dy * dy;
|
||||
dy *= 8.0;
|
||||
dy /= TextureSize.y;
|
||||
dy *= signY;
|
||||
vec2 tc = vec2(texcoord.x, yCoord + dy);
|
||||
#endif
|
||||
|
||||
vec3 colour = texture2D(Texture, tc).rgb;
|
||||
|
||||
#if defined(SCANLINES)
|
||||
#if defined(GAMMA)
|
||||
#if defined(FAKE_GAMMA)
|
||||
colour = colour * colour;
|
||||
#else
|
||||
colour = pow(colour, vec3(INPUT_GAMMA));
|
||||
#endif
|
||||
#endif
|
||||
scanLineWeight *= BLOOM_FACTOR;
|
||||
colour *= scanLineWeight;
|
||||
|
||||
#if defined(GAMMA)
|
||||
#if defined(FAKE_GAMMA)
|
||||
colour = sqrt(colour);
|
||||
#else
|
||||
colour = pow(colour, vec3(1.0/OUTPUT_GAMMA));
|
||||
#endif
|
||||
#endif
|
||||
#endif
|
||||
#if MASK_TYPE == 0
|
||||
gl_FragColor = vec4(colour, 1.0);
|
||||
#else
|
||||
#if MASK_TYPE == 1
|
||||
float whichMask = fract((gl_FragCoord.x*1.0001) * 0.5);
|
||||
vec3 mask;
|
||||
if (whichMask < 0.5)
|
||||
mask = vec3(MASK_BRIGHTNESS, 1.0, MASK_BRIGHTNESS);
|
||||
else
|
||||
mask = vec3(1.0, MASK_BRIGHTNESS, 1.0);
|
||||
#elif MASK_TYPE == 2
|
||||
float whichMask = fract((gl_FragCoord.x*1.0001) * 0.3333333);
|
||||
vec3 mask = vec3(MASK_BRIGHTNESS, MASK_BRIGHTNESS, MASK_BRIGHTNESS);
|
||||
if (whichMask < 0.3333333)
|
||||
mask.x = 1.0;
|
||||
else if (whichMask < 0.6666666)
|
||||
mask.y = 1.0;
|
||||
else
|
||||
mask.z = 1.0;
|
||||
#endif
|
||||
|
||||
gl_FragColor = vec4(colour * mask, 1.0);
|
||||
#endif
|
||||
}
|
||||
}
|
||||
#endif
|
||||
234
data/shaders/crtpi_240.glsl
Normal file
234
data/shaders/crtpi_240.glsl
Normal file
@@ -0,0 +1,234 @@
|
||||
/*
|
||||
crt-pi - A Raspberry Pi friendly CRT shader.
|
||||
|
||||
Copyright (C) 2015-2016 davej
|
||||
|
||||
This program is free software; you can redistribute it and/or modify it
|
||||
under the terms of the GNU General Public License as published by the Free
|
||||
Software Foundation; either version 2 of the License, or (at your option)
|
||||
any later version.
|
||||
|
||||
|
||||
Notes:
|
||||
|
||||
This shader is designed to work well on Raspberry Pi GPUs (i.e. 1080P @ 60Hz on a game with a 4:3 aspect ratio). It pushes the Pi's GPU hard and enabling some features will slow it down so that it is no longer able to match 1080P @ 60Hz. You will need to overclock your Pi to the fastest setting in raspi-config to get the best results from this shader: 'Pi2' for Pi2 and 'Turbo' for original Pi and Pi Zero. Note: Pi2s are slower at running the shader than other Pis, this seems to be down to Pi2s lower maximum memory speed. Pi2s don't quite manage 1080P @ 60Hz - they drop about 1 in 1000 frames. You probably won't notice this, but if you do, try enabling FAKE_GAMMA.
|
||||
|
||||
SCANLINES enables scanlines. You'll almost certainly want to use it with MULTISAMPLE to reduce moire effects. SCANLINE_WEIGHT defines how wide scanlines are (it is an inverse value so a higher number = thinner lines). SCANLINE_GAP_BRIGHTNESS defines how dark the gaps between the scan lines are. Darker gaps between scan lines make moire effects more likely.
|
||||
|
||||
GAMMA enables gamma correction using the values in INPUT_GAMMA and OUTPUT_GAMMA. FAKE_GAMMA causes it to ignore the values in INPUT_GAMMA and OUTPUT_GAMMA and approximate gamma correction in a way which is faster than true gamma whilst still looking better than having none. You must have GAMMA defined to enable FAKE_GAMMA.
|
||||
|
||||
CURVATURE distorts the screen by CURVATURE_X and CURVATURE_Y. Curvature slows things down a lot.
|
||||
|
||||
By default the shader uses linear blending horizontally. If you find this too blury, enable SHARPER.
|
||||
|
||||
BLOOM_FACTOR controls the increase in width for bright scanlines.
|
||||
|
||||
MASK_TYPE defines what, if any, shadow mask to use. MASK_BRIGHTNESS defines how much the mask type darkens the screen.
|
||||
|
||||
*/
|
||||
|
||||
#pragma parameter CURVATURE_X "Screen curvature - horizontal" 0.10 0.0 1.0 0.01
|
||||
#pragma parameter CURVATURE_Y "Screen curvature - vertical" 0.15 0.0 1.0 0.01
|
||||
#pragma parameter MASK_BRIGHTNESS "Mask brightness" 0.70 0.0 1.0 0.01
|
||||
#pragma parameter SCANLINE_WEIGHT "Scanline weight" 6.0 0.0 15.0 0.1
|
||||
#pragma parameter SCANLINE_GAP_BRIGHTNESS "Scanline gap brightness" 0.12 0.0 1.0 0.01
|
||||
#pragma parameter BLOOM_FACTOR "Bloom factor" 1.5 0.0 5.0 0.01
|
||||
#pragma parameter INPUT_GAMMA "Input gamma" 2.4 0.0 5.0 0.01
|
||||
#pragma parameter OUTPUT_GAMMA "Output gamma" 2.2 0.0 5.0 0.01
|
||||
|
||||
// Haven't put these as parameters as it would slow the code down.
|
||||
#define SCANLINES
|
||||
#define MULTISAMPLE
|
||||
#define GAMMA
|
||||
//#define FAKE_GAMMA
|
||||
//#define CURVATURE
|
||||
//#define SHARPER
|
||||
// MASK_TYPE: 0 = none, 1 = green/magenta, 2 = trinitron(ish)
|
||||
#define MASK_TYPE 2
|
||||
|
||||
|
||||
#ifdef GL_ES
|
||||
#define COMPAT_PRECISION mediump
|
||||
precision mediump float;
|
||||
#else
|
||||
#define COMPAT_PRECISION
|
||||
#endif
|
||||
|
||||
#ifdef PARAMETER_UNIFORM
|
||||
uniform COMPAT_PRECISION float CURVATURE_X;
|
||||
uniform COMPAT_PRECISION float CURVATURE_Y;
|
||||
uniform COMPAT_PRECISION float MASK_BRIGHTNESS;
|
||||
uniform COMPAT_PRECISION float SCANLINE_WEIGHT;
|
||||
uniform COMPAT_PRECISION float SCANLINE_GAP_BRIGHTNESS;
|
||||
uniform COMPAT_PRECISION float BLOOM_FACTOR;
|
||||
uniform COMPAT_PRECISION float INPUT_GAMMA;
|
||||
uniform COMPAT_PRECISION float OUTPUT_GAMMA;
|
||||
#else
|
||||
#define CURVATURE_X 0.05
|
||||
#define CURVATURE_Y 0.1
|
||||
#define MASK_BRIGHTNESS 0.80
|
||||
#define SCANLINE_WEIGHT 6.0
|
||||
#define SCANLINE_GAP_BRIGHTNESS 0.12
|
||||
#define BLOOM_FACTOR 3.5
|
||||
#define INPUT_GAMMA 2.4
|
||||
#define OUTPUT_GAMMA 2.2
|
||||
#endif
|
||||
|
||||
/* COMPATIBILITY
|
||||
- GLSL compilers
|
||||
*/
|
||||
|
||||
//uniform vec2 TextureSize;
|
||||
#if defined(CURVATURE)
|
||||
varying vec2 screenScale;
|
||||
#endif
|
||||
varying vec2 TEX0;
|
||||
varying float filterWidth;
|
||||
|
||||
#if defined(VERTEX)
|
||||
//uniform mat4 MVPMatrix;
|
||||
//attribute vec4 VertexCoord;
|
||||
//attribute vec2 TexCoord;
|
||||
//uniform vec2 InputSize;
|
||||
//uniform vec2 OutputSize;
|
||||
|
||||
void main()
|
||||
{
|
||||
#if defined(CURVATURE)
|
||||
screenScale = vec2(1.0, 1.0); //TextureSize / InputSize;
|
||||
#endif
|
||||
filterWidth = (768.0 / 240.0) / 3.0;
|
||||
TEX0 = vec2(gl_MultiTexCoord0.x, 1.0-gl_MultiTexCoord0.y)*1.0001;
|
||||
gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
|
||||
}
|
||||
#elif defined(FRAGMENT)
|
||||
|
||||
uniform sampler2D Texture;
|
||||
|
||||
#if defined(CURVATURE)
|
||||
vec2 Distort(vec2 coord)
|
||||
{
|
||||
vec2 CURVATURE_DISTORTION = vec2(CURVATURE_X, CURVATURE_Y);
|
||||
// Barrel distortion shrinks the display area a bit, this will allow us to counteract that.
|
||||
vec2 barrelScale = 1.0 - (0.23 * CURVATURE_DISTORTION);
|
||||
coord *= screenScale;
|
||||
coord -= vec2(0.5);
|
||||
float rsq = coord.x * coord.x + coord.y * coord.y;
|
||||
coord += coord * (CURVATURE_DISTORTION * rsq);
|
||||
coord *= barrelScale;
|
||||
if (abs(coord.x) >= 0.5 || abs(coord.y) >= 0.5)
|
||||
coord = vec2(-1.0); // If out of bounds, return an invalid value.
|
||||
else
|
||||
{
|
||||
coord += vec2(0.5);
|
||||
coord /= screenScale;
|
||||
}
|
||||
|
||||
return coord;
|
||||
}
|
||||
#endif
|
||||
|
||||
float CalcScanLineWeight(float dist)
|
||||
{
|
||||
return max(1.0-dist*dist*SCANLINE_WEIGHT, SCANLINE_GAP_BRIGHTNESS);
|
||||
}
|
||||
|
||||
float CalcScanLine(float dy)
|
||||
{
|
||||
float scanLineWeight = CalcScanLineWeight(dy);
|
||||
#if defined(MULTISAMPLE)
|
||||
scanLineWeight += CalcScanLineWeight(dy-filterWidth);
|
||||
scanLineWeight += CalcScanLineWeight(dy+filterWidth);
|
||||
scanLineWeight *= 0.3333333;
|
||||
#endif
|
||||
return scanLineWeight;
|
||||
}
|
||||
|
||||
void main()
|
||||
{
|
||||
vec2 TextureSize = vec2(320.0, 240.0);
|
||||
#if defined(CURVATURE)
|
||||
vec2 texcoord = Distort(TEX0);
|
||||
if (texcoord.x < 0.0)
|
||||
gl_FragColor = vec4(0.0);
|
||||
else
|
||||
#else
|
||||
vec2 texcoord = TEX0;
|
||||
#endif
|
||||
{
|
||||
vec2 texcoordInPixels = texcoord * TextureSize;
|
||||
#if defined(SHARPER)
|
||||
vec2 tempCoord = floor(texcoordInPixels) + 0.5;
|
||||
vec2 coord = tempCoord / TextureSize;
|
||||
vec2 deltas = texcoordInPixels - tempCoord;
|
||||
float scanLineWeight = CalcScanLine(deltas.y);
|
||||
vec2 signs = sign(deltas);
|
||||
deltas.x *= 2.0;
|
||||
deltas = deltas * deltas;
|
||||
deltas.y = deltas.y * deltas.y;
|
||||
deltas.x *= 0.5;
|
||||
deltas.y *= 8.0;
|
||||
deltas /= TextureSize;
|
||||
deltas *= signs;
|
||||
vec2 tc = coord + deltas;
|
||||
#else
|
||||
float tempY = floor(texcoordInPixels.y) + 0.5;
|
||||
float yCoord = tempY / TextureSize.y;
|
||||
float dy = texcoordInPixels.y - tempY;
|
||||
float scanLineWeight = CalcScanLine(dy);
|
||||
float signY = sign(dy);
|
||||
dy = dy * dy;
|
||||
dy = dy * dy;
|
||||
dy *= 8.0;
|
||||
dy /= TextureSize.y;
|
||||
dy *= signY;
|
||||
vec2 tc = vec2(texcoord.x, yCoord + dy);
|
||||
#endif
|
||||
|
||||
vec3 colour = texture2D(Texture, tc).rgb;
|
||||
|
||||
#if defined(SCANLINES)
|
||||
#if defined(GAMMA)
|
||||
#if defined(FAKE_GAMMA)
|
||||
colour = colour * colour;
|
||||
#else
|
||||
colour = pow(colour, vec3(INPUT_GAMMA));
|
||||
#endif
|
||||
#endif
|
||||
scanLineWeight *= BLOOM_FACTOR;
|
||||
colour *= scanLineWeight;
|
||||
|
||||
#if defined(GAMMA)
|
||||
#if defined(FAKE_GAMMA)
|
||||
colour = sqrt(colour);
|
||||
#else
|
||||
colour = pow(colour, vec3(1.0/OUTPUT_GAMMA));
|
||||
#endif
|
||||
#endif
|
||||
#endif
|
||||
#if MASK_TYPE == 0
|
||||
gl_FragColor = vec4(colour, 1.0);
|
||||
#else
|
||||
#if MASK_TYPE == 1
|
||||
float whichMask = fract((gl_FragCoord.x*1.0001) * 0.5);
|
||||
vec3 mask;
|
||||
if (whichMask < 0.5)
|
||||
mask = vec3(MASK_BRIGHTNESS, 1.0, MASK_BRIGHTNESS);
|
||||
else
|
||||
mask = vec3(1.0, MASK_BRIGHTNESS, 1.0);
|
||||
#elif MASK_TYPE == 2
|
||||
float whichMask = fract((gl_FragCoord.x*1.0001) * 0.3333333);
|
||||
vec3 mask = vec3(MASK_BRIGHTNESS, MASK_BRIGHTNESS, MASK_BRIGHTNESS);
|
||||
if (whichMask < 0.3333333)
|
||||
mask.x = 1.0;
|
||||
else if (whichMask < 0.6666666)
|
||||
mask.y = 1.0;
|
||||
else
|
||||
mask.z = 1.0;
|
||||
#endif
|
||||
|
||||
gl_FragColor = vec4(colour * mask, 1.0);
|
||||
#endif
|
||||
}
|
||||
}
|
||||
#endif
|
||||
@@ -23,11 +23,11 @@
|
||||
<key>CFBundlePackageType</key>
|
||||
<string>APPL</string>
|
||||
<key>CFBundleShortVersionString</key>
|
||||
<string>1.08</string>
|
||||
<string>1.09</string>
|
||||
<key>CFBundleSignature</key>
|
||||
<string>????</string>
|
||||
<key>CFBundleVersion</key>
|
||||
<string>1.08</string>
|
||||
<string>1.09</string>
|
||||
<key>CSResourcesFileMapped</key>
|
||||
<true/>
|
||||
<key>LSMinimumSystemVersion</key>
|
||||
|
||||
@@ -1,4 +1,8 @@
|
||||
#include "animatedsprite.h"
|
||||
#include <fstream> // Para basic_ostream, operator<<, basic_istream, basic...
|
||||
#include <iostream> // Para cout
|
||||
#include <sstream> // Para basic_stringstream
|
||||
#include "texture.h" // Para Texture
|
||||
|
||||
// Carga la animación desde un fichero
|
||||
animatedSprite_t loadAnimationFromFile(Texture *texture, std::string filePath, bool verbose)
|
||||
@@ -1,15 +1,12 @@
|
||||
#pragma once
|
||||
|
||||
#include <SDL2/SDL.h>
|
||||
#include "movingsprite.h"
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#ifndef ANIMATEDSPRITE_H
|
||||
#define ANIMATEDSPRITE_H
|
||||
#include <SDL2/SDL_rect.h> // Para SDL_Rect
|
||||
#include <SDL2/SDL_render.h> // Para SDL_Renderer
|
||||
#include <SDL2/SDL_stdinc.h> // Para Uint8
|
||||
#include <string> // Para string
|
||||
#include <vector> // Para vector
|
||||
#include "movingsprite.h" // Para MovingSprite
|
||||
class Texture;
|
||||
|
||||
struct animation_t
|
||||
{
|
||||
@@ -98,6 +95,4 @@ public:
|
||||
|
||||
// Reinicia la animación
|
||||
void resetAnimation();
|
||||
};
|
||||
|
||||
#endif
|
||||
};
|
||||
@@ -1,25 +1,41 @@
|
||||
#include "asset.h"
|
||||
#include <iostream>
|
||||
#include <SDL2/SDL_rwops.h> // Para SDL_RWFromFile, SDL_RWclose, SDL_RWops
|
||||
#include <SDL2/SDL_stdinc.h> // Para SDL_max
|
||||
#include <stddef.h> // Para size_t
|
||||
#include <iostream> // Para basic_ostream, operator<<, cout, endl
|
||||
|
||||
// Constructor
|
||||
Asset::Asset(std::string executablePath)
|
||||
// [SINGLETON] Hay que definir las variables estáticas, desde el .h sólo la hemos declarado
|
||||
Asset *Asset::asset_ = nullptr;
|
||||
|
||||
// [SINGLETON] Crearemos el objeto con esta función estática
|
||||
void Asset::init(const std::string &executable_path)
|
||||
{
|
||||
this->executablePath = executablePath.substr(0, executablePath.find_last_of("\\/"));
|
||||
longestName = 0;
|
||||
verbose = true;
|
||||
Asset::asset_ = new Asset(executable_path);
|
||||
}
|
||||
|
||||
// [SINGLETON] Destruiremos el objeto con esta función estática
|
||||
void Asset::destroy()
|
||||
{
|
||||
delete Asset::asset_;
|
||||
}
|
||||
|
||||
// [SINGLETON] Con este método obtenemos el objeto y podemos trabajar con él
|
||||
Asset *Asset::get()
|
||||
{
|
||||
return Asset::asset_;
|
||||
}
|
||||
|
||||
// Añade un elemento a la lista
|
||||
void Asset::add(std::string file, enum assetType type, bool required, bool absolute)
|
||||
{
|
||||
item_t temp;
|
||||
temp.file = absolute ? file : executablePath + file;
|
||||
temp.file = absolute ? file : executable_path_ + file;
|
||||
temp.type = type;
|
||||
temp.required = required;
|
||||
fileList.push_back(temp);
|
||||
|
||||
const std::string filename = file.substr(file.find_last_of("\\/") + 1);
|
||||
longestName = SDL_max(longestName, filename.size());
|
||||
longest_name_ = SDL_max(longest_name_, filename.size());
|
||||
}
|
||||
|
||||
// Devuelve el fichero de un elemento de la lista a partir de una cadena
|
||||
@@ -36,7 +52,7 @@ std::string Asset::get(std::string text)
|
||||
}
|
||||
}
|
||||
|
||||
if (verbose)
|
||||
if (verbose_)
|
||||
{
|
||||
std::cout << "Warning: file " << text.c_str() << " not found" << std::endl;
|
||||
}
|
||||
@@ -48,11 +64,11 @@ bool Asset::check()
|
||||
{
|
||||
bool success = true;
|
||||
|
||||
if (verbose)
|
||||
if (verbose_)
|
||||
{
|
||||
std::cout << "\n** Checking files" << std::endl;
|
||||
|
||||
std::cout << "Executable path is: " << executablePath << std::endl;
|
||||
std::cout << "Executable path is: " << executable_path_ << std::endl;
|
||||
std::cout << "Sample filepath: " << fileList.back().file << std::endl;
|
||||
}
|
||||
|
||||
@@ -73,7 +89,7 @@ bool Asset::check()
|
||||
// Si hay ficheros de ese tipo, comprueba si existen
|
||||
if (any)
|
||||
{
|
||||
if (verbose)
|
||||
if (verbose_)
|
||||
{
|
||||
std::cout << "\n>> " << getTypeName(type).c_str() << " FILES" << std::endl;
|
||||
}
|
||||
@@ -89,7 +105,7 @@ bool Asset::check()
|
||||
}
|
||||
|
||||
// Resultado
|
||||
if (verbose)
|
||||
if (verbose_)
|
||||
{
|
||||
if (success)
|
||||
{
|
||||
@@ -123,11 +139,11 @@ bool Asset::checkFile(std::string path)
|
||||
SDL_RWclose(file);
|
||||
}
|
||||
|
||||
if (verbose)
|
||||
if (verbose_)
|
||||
{
|
||||
std::cout.setf(std::ios::left, std::ios::adjustfield);
|
||||
std::cout << "Checking file: ";
|
||||
std::cout.width(longestName + 2);
|
||||
std::cout.width(longest_name_ + 2);
|
||||
std::cout.fill('.');
|
||||
std::cout << filename + " ";
|
||||
std::cout << " [" + result + "]" << std::endl;
|
||||
@@ -186,5 +202,5 @@ std::string Asset::getTypeName(int type)
|
||||
// Establece si ha de mostrar texto por pantalla
|
||||
void Asset::setVerbose(bool value)
|
||||
{
|
||||
verbose = value;
|
||||
verbose_ = value;
|
||||
}
|
||||
@@ -1,11 +1,8 @@
|
||||
#pragma once
|
||||
|
||||
#include <SDL2/SDL.h>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#ifndef ASSET_H
|
||||
#define ASSET_H
|
||||
#include <string> // Para string
|
||||
#include <vector> // Para vector
|
||||
#include "utils.h"
|
||||
|
||||
enum assetType
|
||||
{
|
||||
@@ -25,20 +22,22 @@ enum assetType
|
||||
class Asset
|
||||
{
|
||||
private:
|
||||
// [SINGLETON] Objeto asset privado
|
||||
static Asset *asset_;
|
||||
|
||||
// Estructura para definir un item
|
||||
struct item_t
|
||||
{
|
||||
std::string file; // Ruta del fichero desde la raiz del directorio
|
||||
enum assetType type; // Indica el tipo de recurso
|
||||
bool required; // Indica si es un fichero que debe de existir
|
||||
//bool absolute; // Indica si la ruta que se ha proporcionado es una ruta absoluta
|
||||
};
|
||||
|
||||
// Variables
|
||||
int longestName; // Contiene la longitud del nombre de fichero mas largo
|
||||
int longest_name_ = 0; // Contiene la longitud del nombre de fichero mas largo
|
||||
std::vector<item_t> fileList; // Listado con todas las rutas a los ficheros
|
||||
std::string executablePath; // Ruta al ejecutable
|
||||
bool verbose; // Indica si ha de mostrar información por pantalla
|
||||
std::string executable_path_; // Ruta al ejecutable
|
||||
bool verbose_ = true; // Indica si ha de mostrar información por pantalla
|
||||
|
||||
// Comprueba que existe un fichero
|
||||
bool checkFile(std::string executablePath);
|
||||
@@ -46,9 +45,22 @@ private:
|
||||
// Devuelve el nombre del tipo de recurso
|
||||
std::string getTypeName(int type);
|
||||
|
||||
public:
|
||||
// Constructor
|
||||
Asset(std::string path);
|
||||
explicit Asset(const std::string &executable_path)
|
||||
: executable_path_(getPath(executable_path)) {}
|
||||
|
||||
// Destructor
|
||||
~Asset() = default;
|
||||
|
||||
public:
|
||||
// [SINGLETON] Crearemos el objeto con esta función estática
|
||||
static void init(const std::string &executable_path);
|
||||
|
||||
// [SINGLETON] Destruiremos el objeto con esta función estática
|
||||
static void destroy();
|
||||
|
||||
// [SINGLETON] Con este método obtenemos el objeto y podemos trabajar con él
|
||||
static Asset *get();
|
||||
|
||||
// Añade un elemento a la lista
|
||||
void add(std::string file, enum assetType type, bool required = true, bool absolute = false);
|
||||
@@ -62,5 +74,3 @@ public:
|
||||
// Establece si ha de mostrar texto por pantalla
|
||||
void setVerbose(bool value);
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -1,111 +1,72 @@
|
||||
#include "cheevos.h"
|
||||
#include <iostream>
|
||||
#include <SDL2/SDL_error.h> // Para SDL_GetError
|
||||
#include <SDL2/SDL_rwops.h> // Para SDL_RWFromFile, SDL_RWclose, SDL_RWwrite
|
||||
#include <stddef.h> // Para NULL
|
||||
#include <iostream> // Para basic_ostream, operator<<, cout, endl
|
||||
#include "notifier.h" // Para Screen
|
||||
#include "utils.h" // Para options_t
|
||||
#include "options.h"
|
||||
#include <fstream> // Para fstream
|
||||
|
||||
// [SINGLETON]
|
||||
Cheevos *Cheevos::cheevos_ = nullptr;
|
||||
|
||||
// [SINGLETON] Crearemos el objeto con esta función estática
|
||||
void Cheevos::init(const std::string &file)
|
||||
{
|
||||
Cheevos::cheevos_ = new Cheevos(file);
|
||||
}
|
||||
|
||||
// [SINGLETON] Destruiremos el objeto con esta función estática
|
||||
void Cheevos::destroy()
|
||||
{
|
||||
delete Cheevos::cheevos_;
|
||||
}
|
||||
|
||||
// [SINGLETON] Con este método obtenemos el objeto y podemos trabajar con él
|
||||
Cheevos *Cheevos::get()
|
||||
{
|
||||
return Cheevos::cheevos_;
|
||||
}
|
||||
|
||||
// Constructor
|
||||
Cheevos::Cheevos(Screen *screen, options_t *options, std::string file, Online *online)
|
||||
Cheevos::Cheevos(const std::string &file)
|
||||
: file_(file)
|
||||
{
|
||||
// Copia la dirección de los objetos
|
||||
this->options = options;
|
||||
this->screen = screen;
|
||||
this->file = file;
|
||||
this->online = online;
|
||||
|
||||
// Inicializa los logros
|
||||
init();
|
||||
|
||||
// Inicializa variables
|
||||
enabled = true;
|
||||
|
||||
// Carga el estado de los logros
|
||||
load();
|
||||
loadFromFile();
|
||||
}
|
||||
|
||||
// Destructor
|
||||
Cheevos::~Cheevos()
|
||||
{
|
||||
// Guarda el estado de los logros
|
||||
save();
|
||||
|
||||
cheevos.clear();
|
||||
saveToFile();
|
||||
}
|
||||
|
||||
// Inicializa los logros
|
||||
void Cheevos::init()
|
||||
{
|
||||
cheevos.clear();
|
||||
|
||||
cheevos_t c;
|
||||
c.completed = false;
|
||||
c.valid = true;
|
||||
c.icon = 2;
|
||||
|
||||
c.id = 1;
|
||||
c.caption = "SHINY THINGS";
|
||||
c.description = "Get 25\% of the items";
|
||||
cheevos.push_back(c);
|
||||
|
||||
c.id = 2;
|
||||
c.caption = "HALF THE WORK";
|
||||
c.description = "Get 50\% of the items";
|
||||
cheevos.push_back(c);
|
||||
|
||||
c.id = 3;
|
||||
c.caption = "GETTING THERE";
|
||||
c.description = "Get 75\% of the items";
|
||||
cheevos.push_back(c);
|
||||
|
||||
c.id = 4;
|
||||
c.caption = "THE COLLECTOR";
|
||||
c.description = "Get 100\% of the items";
|
||||
cheevos.push_back(c);
|
||||
|
||||
c.id = 5;
|
||||
c.caption = "WANDERING AROUND";
|
||||
c.description = "Visit 20 rooms";
|
||||
cheevos.push_back(c);
|
||||
|
||||
c.id = 6;
|
||||
c.caption = "I GOT LOST";
|
||||
c.description = "Visit 40 rooms";
|
||||
cheevos.push_back(c);
|
||||
|
||||
c.id = 7;
|
||||
c.caption = "I LIKE TO EXPLORE";
|
||||
c.description = "Visit all rooms";
|
||||
cheevos.push_back(c);
|
||||
|
||||
c.id = 8;
|
||||
c.caption = "FINISH THE GAME";
|
||||
c.description = "Complete the game";
|
||||
cheevos.push_back(c);
|
||||
|
||||
c.id = 9;
|
||||
c.caption = "I WAS SUCKED BY A HOLE";
|
||||
c.description = "Complete the game without entering the jail";
|
||||
cheevos.push_back(c);
|
||||
|
||||
c.id = 10;
|
||||
c.caption = "MY LITTLE PROJECTS";
|
||||
c.description = "Complete the game with all items";
|
||||
cheevos.push_back(c);
|
||||
|
||||
c.id = 11;
|
||||
c.caption = "I LIKE MY MULTICOLOURED FRIENDS";
|
||||
c.description = "Complete the game without dying";
|
||||
cheevos.push_back(c);
|
||||
|
||||
c.id = 12;
|
||||
c.caption = "SHIT PROJECTS DONE FAST";
|
||||
c.description = "Complete the game in under 30 minutes";
|
||||
cheevos.push_back(c);
|
||||
cheevos_list_.clear();
|
||||
cheevos_list_.emplace_back(1, "SHINY THINGS", "Get 25% of the items", 2);
|
||||
cheevos_list_.emplace_back(2, "HALF THE WORK", "Get 50% of the items", 2);
|
||||
cheevos_list_.emplace_back(3, "GETTING THERE", "Get 75% of the items", 2);
|
||||
cheevos_list_.emplace_back(4, "THE COLLECTOR", "Get 100% of the items", 2);
|
||||
cheevos_list_.emplace_back(5, "WANDERING AROUND", "Visit 20 rooms", 2);
|
||||
cheevos_list_.emplace_back(6, "I GOT LOST", "Visit 40 rooms", 2);
|
||||
cheevos_list_.emplace_back(7, "I LIKE TO EXPLORE", "Visit all rooms", 2);
|
||||
cheevos_list_.emplace_back(8, "FINISH THE GAME", "Complete the game", 2);
|
||||
cheevos_list_.emplace_back(9, "I WAS SUCKED BY A HOLE", "Complete the game without entering the jail", 2);
|
||||
cheevos_list_.emplace_back(10, "MY LITTLE PROJECTS", "Complete the game with all items", 2);
|
||||
cheevos_list_.emplace_back(11, "I LIKE MY MULTICOLOURED FRIENDS", "Complete the game without dying", 2);
|
||||
cheevos_list_.emplace_back(12, "SHIT PROJECTS DONE FAST", "Complete the game in under 30 minutes", 2);
|
||||
}
|
||||
|
||||
// Busca un logro por id y devuelve el indice
|
||||
int Cheevos::find(int id)
|
||||
{
|
||||
for (int i = 0; i < (int)cheevos.size(); ++i)
|
||||
for (int i = 0; i < (int)cheevos_list_.size(); ++i)
|
||||
{
|
||||
if (cheevos[i].id == id)
|
||||
if (cheevos_list_[i].id == id)
|
||||
{
|
||||
return i;
|
||||
}
|
||||
@@ -119,130 +80,82 @@ void Cheevos::unlock(int id)
|
||||
{
|
||||
const int index = find(id);
|
||||
|
||||
if (index == -1)
|
||||
// Si el índice es inválido, el logro no es válido, ya está completado o el sistema de logros no está habilitado, no hacemos nada
|
||||
if (index == -1 || !cheevos_list_.at(index).valid || cheevos_list_.at(index).completed || !enabled_)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (!cheevos[index].valid)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (cheevos[index].completed)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (!enabled)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
cheevos[index].completed = true;
|
||||
screen->showNotification("ACHIEVEMENT UNLOCKED!", cheevos[index].caption, cheevos[index].icon);
|
||||
save();
|
||||
// Marcar el logro como completado
|
||||
cheevos_list_.at(index).completed = true;
|
||||
// Mostrar notificación en la pantalla
|
||||
Notifier::get()->show("ACHIEVEMENT UNLOCKED!", cheevos_list_.at(index).caption, cheevos_list_.at(index).icon);
|
||||
// Guardar el estado de los logros
|
||||
saveToFile();
|
||||
}
|
||||
|
||||
// Invalida un logro
|
||||
void Cheevos::invalidate(int id)
|
||||
{
|
||||
const int index = find(id);
|
||||
if (index == -1)
|
||||
|
||||
// Si el índice es válido, se invalida el logro
|
||||
if (index != -1)
|
||||
{
|
||||
return;
|
||||
}
|
||||
cheevos[index].valid = false;
|
||||
}
|
||||
|
||||
// Habilita o deshabilita los logros
|
||||
void Cheevos::enable(bool value)
|
||||
{
|
||||
enabled = value;
|
||||
}
|
||||
|
||||
// Carga el estado de los logros
|
||||
void Cheevos::load()
|
||||
{
|
||||
if (options->online.enabled)
|
||||
{ // Carga el estado de los logros desde el servidor online
|
||||
loadFromServer();
|
||||
}
|
||||
else
|
||||
{ // Carga el estado de los logros desde un fichero
|
||||
loadFromFile();
|
||||
}
|
||||
}
|
||||
|
||||
// Guarda el estado de los logros
|
||||
void Cheevos::save()
|
||||
{
|
||||
if (options->online.enabled)
|
||||
{ // Guarda el estado de los logros en el servidor online
|
||||
saveToServer();
|
||||
}
|
||||
else
|
||||
{ // Guarda el estado de los logros en un fichero
|
||||
saveToFile();
|
||||
cheevos_list_.at(index).valid = false;
|
||||
}
|
||||
}
|
||||
|
||||
// Carga el estado de los logros desde un fichero
|
||||
void Cheevos::loadFromFile()
|
||||
{
|
||||
// Abre el fichero en modo lectura (binario)
|
||||
SDL_RWops *file = SDL_RWFromFile(this->file.c_str(), "r+b");
|
||||
std::ifstream file(this->file_, std::ios::binary);
|
||||
|
||||
// El fichero no existe
|
||||
if (file == NULL)
|
||||
if (!file)
|
||||
{
|
||||
if (options->console)
|
||||
if (options.console)
|
||||
{
|
||||
std::cout << "Warning: Unable to open file! SDL Error: " << SDL_GetError() << std::endl;
|
||||
std::cout << "Warning: Unable to open file! Creating new file..." << std::endl;
|
||||
}
|
||||
|
||||
// Crea el fichero en modo escritura (binario)
|
||||
file = SDL_RWFromFile(this->file.c_str(), "w+b");
|
||||
std::ofstream newFile(this->file_, std::ios::binary);
|
||||
|
||||
if (file != NULL)
|
||||
if (newFile)
|
||||
{
|
||||
if (options->console)
|
||||
if (options.console)
|
||||
{
|
||||
std::cout << "New file created!" << std::endl;
|
||||
}
|
||||
|
||||
// Guarda la información
|
||||
for (int i = 0; i < (int)cheevos.size(); ++i)
|
||||
for (const auto &cheevo : cheevos_list_)
|
||||
{
|
||||
SDL_RWwrite(file, &cheevos[i].completed, sizeof(bool), 1);
|
||||
newFile.write(reinterpret_cast<const char *>(&cheevo.completed), sizeof(bool));
|
||||
}
|
||||
|
||||
// Cierra el fichero
|
||||
SDL_RWclose(file);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (options->console)
|
||||
if (options.console)
|
||||
{
|
||||
std::cout << "Error: Unable to create file! SDL Error: " << SDL_GetError() << std::endl;
|
||||
std::cerr << "Error: Unable to create file!" << std::endl;
|
||||
}
|
||||
}
|
||||
}
|
||||
// El fichero existe
|
||||
else
|
||||
{
|
||||
// Carga los datos
|
||||
if (options->console)
|
||||
if (options.console)
|
||||
{
|
||||
std::cout << "Reading file...!" << std::endl;
|
||||
}
|
||||
for (int i = 0; i < (int)cheevos.size(); ++i)
|
||||
{
|
||||
SDL_RWread(file, &cheevos[i].completed, sizeof(bool), 1);
|
||||
}
|
||||
|
||||
// Cierra el fichero
|
||||
SDL_RWclose(file);
|
||||
// Carga los datos
|
||||
for (auto &cheevo : cheevos_list_)
|
||||
{
|
||||
file.read(reinterpret_cast<char *>(&cheevo.completed), sizeof(bool));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -250,13 +163,13 @@ void Cheevos::loadFromFile()
|
||||
void Cheevos::saveToFile()
|
||||
{
|
||||
// Abre el fichero en modo escritura (binario)
|
||||
SDL_RWops *file = SDL_RWFromFile(this->file.c_str(), "w+b");
|
||||
SDL_RWops *file = SDL_RWFromFile(this->file_.c_str(), "w+b");
|
||||
if (file != NULL)
|
||||
{
|
||||
// Guarda la información
|
||||
for (int i = 0; i < (int)cheevos.size(); ++i)
|
||||
for (int i = 0; i < (int)cheevos_list_.size(); ++i)
|
||||
{
|
||||
SDL_RWwrite(file, &cheevos[i].completed, sizeof(bool), 1);
|
||||
SDL_RWwrite(file, &cheevos_list_[i].completed, sizeof(bool), 1);
|
||||
}
|
||||
|
||||
// Cierra el fichero
|
||||
@@ -264,76 +177,23 @@ void Cheevos::saveToFile()
|
||||
}
|
||||
else
|
||||
{
|
||||
if (options->console)
|
||||
if (options.console)
|
||||
{
|
||||
std::cout << "Error: Unable to save file! " << SDL_GetError() << std::endl;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Lista los logros
|
||||
std::vector<cheevos_t> Cheevos::list()
|
||||
{
|
||||
return cheevos;
|
||||
}
|
||||
|
||||
// Devuelve el número total de logros desbloqueados
|
||||
int Cheevos::unlocked()
|
||||
{
|
||||
int count = 0;
|
||||
for (auto cheevo : cheevos)
|
||||
for (auto cheevo : cheevos_list_)
|
||||
{
|
||||
if (cheevo.completed)
|
||||
{
|
||||
count++;
|
||||
}
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
// Devuelve el número total de logros
|
||||
int Cheevos::count()
|
||||
{
|
||||
return cheevos.size();
|
||||
}
|
||||
|
||||
// Carga el estado de los logros desde el servidor online
|
||||
void Cheevos::loadFromServer()
|
||||
{
|
||||
std::string cheevosData = online->getCheevos();
|
||||
|
||||
// Gestiona los posibles errores
|
||||
const bool noData = cheevosData == "" ? true : false;
|
||||
const bool incompleteData = cheevosData.length() != cheevos.size() ? true : false;
|
||||
if (noData || incompleteData)
|
||||
{
|
||||
// Pone todos los logros en incompleto
|
||||
init();
|
||||
return;
|
||||
}
|
||||
|
||||
// Asigna los valores leídos desde el servidor
|
||||
for (int i = 0; i < (int)cheevosData.length(); ++i)
|
||||
{
|
||||
bool value = cheevosData.at(i) == '1' ? true : false;
|
||||
cheevos.at(i).completed = value;
|
||||
}
|
||||
}
|
||||
|
||||
// Guarda el estado de los logros en el servidor online
|
||||
void Cheevos::saveToServer()
|
||||
{
|
||||
std::string cheevosData = "";
|
||||
// cheevos[2].completed = true;
|
||||
for (auto cheevo : cheevos)
|
||||
{
|
||||
std::string data = cheevo.completed ? "1" : "0";
|
||||
cheevosData.append(data);
|
||||
}
|
||||
|
||||
online->setCheevos(cheevosData);
|
||||
}
|
||||
|
||||
// Vuelve a cargar los logros desde el origen
|
||||
void Cheevos::reload()
|
||||
{
|
||||
load();
|
||||
}
|
||||
@@ -1,15 +1,11 @@
|
||||
#pragma once
|
||||
#include <SDL2/SDL.h>
|
||||
#include "jail_engine/screen.h"
|
||||
#include "jail_engine/utils.h"
|
||||
#include "online.h"
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <string> // Para string
|
||||
#include <vector> // Para vector
|
||||
class Screen;
|
||||
struct options_t;
|
||||
|
||||
#ifndef CHEEVOS_H
|
||||
#define CHEEVOS_H
|
||||
|
||||
struct cheevos_t
|
||||
// Struct para los logros
|
||||
struct Achievement
|
||||
{
|
||||
int id; // Identificador del logro
|
||||
std::string caption; // Texto con el nombre del logro
|
||||
@@ -17,52 +13,54 @@ struct cheevos_t
|
||||
int icon; // Indice del icono a utilizar en la notificación
|
||||
bool completed; // Indica si se ha obtenido el logro
|
||||
bool valid; // Indica si se puede obtener el logro
|
||||
|
||||
// Constructor vacío
|
||||
Achievement() : id(0), icon(0), completed(false), valid(true) {}
|
||||
|
||||
// Constructor parametrizado
|
||||
Achievement(int id, const std::string &caption, const std::string &description, int icon, bool completed = false, bool valid = true)
|
||||
: id(id), caption(caption), description(description), icon(icon), completed(completed), valid(valid) {}
|
||||
};
|
||||
|
||||
class Cheevos
|
||||
{
|
||||
private:
|
||||
// Punteros y objetos
|
||||
Screen *screen; // Objeto encargado de dibujar en pantalla
|
||||
options_t *options; // Puntero a las opciones del juego
|
||||
Online *online; // Objeto para gestionar la lectura y escritura de datos en el servidor remoto
|
||||
// [SINGLETON] Objeto privado
|
||||
static Cheevos *cheevos_;
|
||||
|
||||
// Variables
|
||||
std::vector<cheevos_t> cheevos; // Listado de logros
|
||||
bool enabled; // Indica si los logros se pueden obtener
|
||||
std::string file; // Fichero done leer/almacenar el estado de los logros
|
||||
std::vector<Achievement> cheevos_list_; // Listado de logros
|
||||
bool enabled_ = true; // Indica si los logros se pueden obtener
|
||||
std::string file_; // Fichero donde leer/almacenar el estado de los logros
|
||||
|
||||
// Inicializa los logros
|
||||
void init();
|
||||
|
||||
// Busca un logro por id y devuelve el indice
|
||||
// Busca un logro por id y devuelve el índice
|
||||
int find(int id);
|
||||
|
||||
// Carga el estado de los logros
|
||||
void load();
|
||||
|
||||
// Guarda el estado de los logros
|
||||
void save();
|
||||
|
||||
// Carga el estado de los logros desde un fichero
|
||||
void loadFromFile();
|
||||
|
||||
// Guarda el estado de los logros en un fichero
|
||||
void saveToFile();
|
||||
|
||||
// Carga el estado de los logros desde el servidor online
|
||||
void loadFromServer();
|
||||
|
||||
// Guarda el estado de los logros en el servidor online
|
||||
void saveToServer();
|
||||
|
||||
public:
|
||||
// Constructor
|
||||
Cheevos(Screen *screen, options_t *options, std::string file, Online *online);
|
||||
Cheevos(const std::string &file);
|
||||
|
||||
// Destructor
|
||||
~Cheevos();
|
||||
|
||||
public:
|
||||
// [SINGLETON] Crearemos el objeto con esta función estática
|
||||
static void init(const std::string &file);
|
||||
|
||||
// [SINGLETON] Destruiremos el objeto con esta función estática
|
||||
static void destroy();
|
||||
|
||||
// [SINGLETON] Con este método obtenemos el objeto y podemos trabajar con él
|
||||
static Cheevos *get();
|
||||
|
||||
// Desbloquea un logro
|
||||
void unlock(int id);
|
||||
|
||||
@@ -70,19 +68,14 @@ public:
|
||||
void invalidate(int id);
|
||||
|
||||
// Habilita o deshabilita los logros
|
||||
void enable(bool value);
|
||||
void enable(bool value) { enabled_ = value; }
|
||||
|
||||
// Lista los logros
|
||||
std::vector<cheevos_t> list();
|
||||
std::vector<Achievement> list() { return cheevos_list_; }
|
||||
|
||||
// Devuelve el número total de logros desbloqueados
|
||||
int unlocked();
|
||||
|
||||
// Devuelve el número total de logros
|
||||
int count();
|
||||
|
||||
// Vuelve a cargar los logros desde el origen
|
||||
void reload();
|
||||
int count() { return cheevos_list_.size(); }
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
@@ -2,71 +2,66 @@
|
||||
|
||||
#include <SDL2/SDL.h>
|
||||
#include <string>
|
||||
#include "jail_engine/utils.h"
|
||||
|
||||
#ifndef CONST_H
|
||||
#define CONST_H
|
||||
#include "utils.h"
|
||||
|
||||
// Textos
|
||||
#define WINDOW_CAPTION "JailDoctor's Dilemma"
|
||||
#define TEXT_COPYRIGHT "@2022 JailDesigner"
|
||||
#define VERSION "0.7"
|
||||
constexpr const char* WINDOW_CAPTION = "JailDoctor's Dilemma";
|
||||
constexpr const char* TEXT_COPYRIGHT = "@2022 JailDesigner";
|
||||
constexpr const char* VERSION = "0.7";
|
||||
|
||||
// Tamaño de bloque
|
||||
#define BLOCK 8
|
||||
#define HALF_BLOCK 4
|
||||
constexpr int BLOCK = 8;
|
||||
constexpr int HALF_BLOCK = 4;
|
||||
|
||||
// Tamaño de la pantalla virtual
|
||||
#define GAMECANVAS_WIDTH 256
|
||||
#define GAMECANVAS_HEIGHT 192
|
||||
constexpr int GAMECANVAS_WIDTH = 256;
|
||||
constexpr int GAMECANVAS_HEIGHT = 192;
|
||||
|
||||
// Zona de juego
|
||||
const int PLAY_AREA_TOP = (0 * BLOCK);
|
||||
const int PLAY_AREA_BOTTOM = (16 * BLOCK);
|
||||
const int PLAY_AREA_LEFT = (0 * BLOCK);
|
||||
const int PLAY_AREA_RIGHT = (32 * BLOCK);
|
||||
const int PLAY_AREA_WIDTH = PLAY_AREA_RIGHT - PLAY_AREA_LEFT;
|
||||
const int PLAY_AREA_HEIGHT = PLAY_AREA_BOTTOM - PLAY_AREA_TOP;
|
||||
const int PLAY_AREA_CENTER_X = PLAY_AREA_LEFT + (PLAY_AREA_WIDTH / 2);
|
||||
const int PLAY_AREA_CENTER_FIRST_QUARTER_X = (PLAY_AREA_WIDTH / 4);
|
||||
const int PLAY_AREA_CENTER_THIRD_QUARTER_X = (PLAY_AREA_WIDTH / 4) * 3;
|
||||
const int PLAY_AREA_CENTER_Y = PLAY_AREA_TOP + (PLAY_AREA_HEIGHT / 2);
|
||||
const int PLAY_AREA_FIRST_QUARTER_Y = PLAY_AREA_HEIGHT / 4;
|
||||
const int PLAY_AREA_THIRD_QUARTER_Y = (PLAY_AREA_HEIGHT / 4) * 3;
|
||||
constexpr int PLAY_AREA_TOP = (0 * BLOCK);
|
||||
constexpr int PLAY_AREA_BOTTOM = (16 * BLOCK);
|
||||
constexpr int PLAY_AREA_LEFT = (0 * BLOCK);
|
||||
constexpr int PLAY_AREA_RIGHT = (32 * BLOCK);
|
||||
constexpr int PLAY_AREA_WIDTH = PLAY_AREA_RIGHT - PLAY_AREA_LEFT;
|
||||
constexpr int PLAY_AREA_HEIGHT = PLAY_AREA_BOTTOM - PLAY_AREA_TOP;
|
||||
constexpr int PLAY_AREA_CENTER_X = PLAY_AREA_LEFT + (PLAY_AREA_WIDTH / 2);
|
||||
constexpr int PLAY_AREA_CENTER_FIRST_QUARTER_X = (PLAY_AREA_WIDTH / 4);
|
||||
constexpr int PLAY_AREA_CENTER_THIRD_QUARTER_X = (PLAY_AREA_WIDTH / 4) * 3;
|
||||
constexpr int PLAY_AREA_CENTER_Y = PLAY_AREA_TOP + (PLAY_AREA_HEIGHT / 2);
|
||||
constexpr int PLAY_AREA_FIRST_QUARTER_Y = PLAY_AREA_HEIGHT / 4;
|
||||
constexpr int PLAY_AREA_THIRD_QUARTER_Y = (PLAY_AREA_HEIGHT / 4) * 3;
|
||||
|
||||
#define BORDER_TOP 0
|
||||
#define BORDER_RIGHT 1
|
||||
#define BORDER_BOTTOM 2
|
||||
#define BORDER_LEFT 3
|
||||
constexpr int BORDER_TOP = 0;
|
||||
constexpr int BORDER_RIGHT = 1;
|
||||
constexpr int BORDER_BOTTOM = 2;
|
||||
constexpr int BORDER_LEFT = 3;
|
||||
|
||||
// Anclajes de pantalla
|
||||
const int GAMECANVAS_CENTER_X = GAMECANVAS_WIDTH / 2;
|
||||
const int GAMECANVAS_FIRST_QUARTER_X = GAMECANVAS_WIDTH / 4;
|
||||
const int GAMECANVAS_THIRD_QUARTER_X = (GAMECANVAS_WIDTH / 4) * 3;
|
||||
const int GAMECANVAS_CENTER_Y = GAMECANVAS_HEIGHT / 2;
|
||||
const int GAMECANVAS_FIRST_QUARTER_Y = GAMECANVAS_HEIGHT / 4;
|
||||
const int GAMECANVAS_THIRD_QUARTER_Y = (GAMECANVAS_HEIGHT / 4) * 3;
|
||||
constexpr int GAMECANVAS_CENTER_X = GAMECANVAS_WIDTH / 2;
|
||||
constexpr int GAMECANVAS_FIRST_QUARTER_X = GAMECANVAS_WIDTH / 4;
|
||||
constexpr int GAMECANVAS_THIRD_QUARTER_X = (GAMECANVAS_WIDTH / 4) * 3;
|
||||
constexpr int GAMECANVAS_CENTER_Y = GAMECANVAS_HEIGHT / 2;
|
||||
constexpr int GAMECANVAS_FIRST_QUARTER_Y = GAMECANVAS_HEIGHT / 4;
|
||||
constexpr int GAMECANVAS_THIRD_QUARTER_Y = (GAMECANVAS_HEIGHT / 4) * 3;
|
||||
|
||||
// Secciones del programa
|
||||
#define SECTION_LOGO 0
|
||||
#define SECTION_LOADING_SCREEN 1
|
||||
#define SECTION_TITLE 2
|
||||
#define SECTION_CREDITS 3
|
||||
#define SECTION_GAME 4
|
||||
#define SECTION_DEMO 5
|
||||
#define SECTION_GAME_OVER 6
|
||||
#define SECTION_ENDING 7
|
||||
#define SECTION_ENDING2 8
|
||||
#define SECTION_QUIT 9
|
||||
constexpr int SECTION_LOGO = 0;
|
||||
constexpr int SECTION_LOADING_SCREEN = 1;
|
||||
constexpr int SECTION_TITLE = 2;
|
||||
constexpr int SECTION_CREDITS = 3;
|
||||
constexpr int SECTION_GAME = 4;
|
||||
constexpr int SECTION_DEMO = 5;
|
||||
constexpr int SECTION_GAME_OVER = 6;
|
||||
constexpr int SECTION_ENDING = 7;
|
||||
constexpr int SECTION_ENDING2 = 8;
|
||||
constexpr int SECTION_QUIT = 9;
|
||||
|
||||
// Subsecciones
|
||||
#define SUBSECTION_LOGO_TO_INTRO 0
|
||||
#define SUBSECTION_LOGO_TO_TITLE 1
|
||||
#define SUBSECTION_TITLE_WITH_LOADING_SCREEN 2
|
||||
#define SUBSECTION_TITLE_WITHOUT_LOADING_SCREEN 3
|
||||
constexpr int SUBSECTION_LOGO_TO_INTRO = 0;
|
||||
constexpr int SUBSECTION_LOGO_TO_TITLE = 1;
|
||||
constexpr int SUBSECTION_TITLE_WITH_LOADING_SCREEN = 2;
|
||||
constexpr int SUBSECTION_TITLE_WITHOUT_LOADING_SCREEN = 3;
|
||||
|
||||
// Colores
|
||||
const color_t borderColor = {0x27, 0x27, 0x36};
|
||||
const color_t black = {0xFF, 0xFF, 0xFF};
|
||||
|
||||
#endif
|
||||
363
source/credits.cpp
Normal file
363
source/credits.cpp
Normal file
@@ -0,0 +1,363 @@
|
||||
#include "credits.h"
|
||||
#include <SDL2/SDL_blendmode.h> // Para SDL_BLENDMODE_BLEND
|
||||
#include <SDL2/SDL_error.h> // Para SDL_GetError
|
||||
#include <SDL2/SDL_pixels.h> // Para SDL_PIXELFORMAT_RGBA8888
|
||||
#include <SDL2/SDL_rect.h> // Para SDL_Rect
|
||||
#include <SDL2/SDL_timer.h> // Para SDL_GetTicks
|
||||
#include <algorithm> // Para min
|
||||
#include <iostream> // Para char_traits, basic_ostream, operator<<
|
||||
#include "animatedsprite.h" // Para AnimatedSprite
|
||||
#include "const.h" // Para GAMECANVAS_HEIGHT, GAMECANVAS_WIDTH
|
||||
#include "input.h" // Para Input, REPEAT_FALSE, inputs_e
|
||||
#include "resource.h" // Para Resource
|
||||
#include "screen.h" // Para Screen
|
||||
#include "text.h" // Para Text, TXT_CENTER, TXT_COLOR
|
||||
#include "asset.h"
|
||||
#include "options.h"
|
||||
class Asset;
|
||||
|
||||
// Constructor
|
||||
Credits::Credits()
|
||||
: screen_(Screen::get()),
|
||||
renderer_(Screen::get()->getRenderer()),
|
||||
resource_(Resource::get()),
|
||||
asset_(Asset::get()),
|
||||
input_(Input::get())
|
||||
{
|
||||
// Reserva memoria para los punteros
|
||||
text_ = new Text(resource_->getOffset("smb2.txt"), resource_->getTexture("smb2.png"), renderer_);
|
||||
sprite_ = new AnimatedSprite(renderer_, resource_->getAnimation("shine.ani"));
|
||||
|
||||
// Inicializa variables
|
||||
options.section.name = SECTION_CREDITS;
|
||||
options.section.subsection = 0;
|
||||
sprite_->setRect({194, 174, 8, 8});
|
||||
|
||||
// Cambia el color del borde
|
||||
screen_->setBorderColor(stringToColor(options.palette, "black"));
|
||||
|
||||
// Crea la textura para el texto que se escribe en pantalla
|
||||
text_texture_ = SDL_CreateTexture(renderer_, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, GAMECANVAS_WIDTH, GAMECANVAS_HEIGHT);
|
||||
if (text_texture_ == nullptr)
|
||||
{
|
||||
if (options.console)
|
||||
{
|
||||
std::cout << "Error: textTexture could not be created!\nSDL Error: " << SDL_GetError() << std::endl;
|
||||
}
|
||||
}
|
||||
SDL_SetTextureBlendMode(text_texture_, SDL_BLENDMODE_BLEND);
|
||||
|
||||
// Crea la textura para cubrir el rexto
|
||||
cover_texture_ = SDL_CreateTexture(renderer_, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, GAMECANVAS_WIDTH, GAMECANVAS_HEIGHT);
|
||||
if (cover_texture_ == nullptr)
|
||||
{
|
||||
if (options.console)
|
||||
{
|
||||
std::cout << "Error: textTexture could not be created!\nSDL Error: " << SDL_GetError() << std::endl;
|
||||
}
|
||||
}
|
||||
SDL_SetTextureBlendMode(cover_texture_, SDL_BLENDMODE_BLEND);
|
||||
|
||||
// Escribe el texto en la textura
|
||||
fillTexture();
|
||||
}
|
||||
|
||||
// Destructor
|
||||
Credits::~Credits()
|
||||
{
|
||||
delete text_;
|
||||
delete sprite_;
|
||||
SDL_DestroyTexture(text_texture_);
|
||||
SDL_DestroyTexture(cover_texture_);
|
||||
}
|
||||
|
||||
// Comprueba el manejador de eventos
|
||||
void Credits::checkEvents()
|
||||
{
|
||||
SDL_Event event;
|
||||
while (SDL_PollEvent(&event))
|
||||
{
|
||||
// Evento de salida de la aplicación
|
||||
if (event.type == SDL_QUIT)
|
||||
{
|
||||
options.section.name = SECTION_QUIT;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Comprueba las entradas
|
||||
void Credits::checkInput()
|
||||
{
|
||||
|
||||
if (input_->checkInput(input_exit, REPEAT_FALSE))
|
||||
{
|
||||
options.section.name = SECTION_QUIT;
|
||||
}
|
||||
|
||||
else if (input_->checkInput(input_toggle_border, REPEAT_FALSE))
|
||||
{
|
||||
screen_->toggleBorder();
|
||||
}
|
||||
|
||||
else if (input_->checkInput(input_toggle_videomode, REPEAT_FALSE))
|
||||
{
|
||||
screen_->toggleVideoMode();
|
||||
}
|
||||
|
||||
else if (input_->checkInput(input_window_dec_size, REPEAT_FALSE))
|
||||
{
|
||||
screen_->decWindowSize();
|
||||
}
|
||||
|
||||
else if (input_->checkInput(input_window_inc_size, REPEAT_FALSE))
|
||||
{
|
||||
screen_->incWindowSize();
|
||||
}
|
||||
|
||||
else if (input_->checkInput(input_toggle_palette, REPEAT_FALSE))
|
||||
{
|
||||
switchPalette();
|
||||
}
|
||||
|
||||
else if (input_->checkInput(input_pause, REPEAT_FALSE) || input_->checkInput(input_accept, REPEAT_FALSE) || input_->checkInput(input_jump, REPEAT_FALSE))
|
||||
{
|
||||
options.section.name = SECTION_TITLE;
|
||||
options.section.subsection = 0;
|
||||
}
|
||||
}
|
||||
|
||||
// Inicializa los textos
|
||||
void Credits::iniTexts()
|
||||
{
|
||||
std::string keys = "";
|
||||
if (options.keys == ctrl_cursor)
|
||||
{
|
||||
keys = "CURSORS";
|
||||
}
|
||||
else if (options.keys == ctrl_opqa)
|
||||
{
|
||||
keys = "O,P AND Q";
|
||||
}
|
||||
else
|
||||
{
|
||||
keys = "A,D AND W";
|
||||
}
|
||||
|
||||
#ifndef GAME_CONSOLE
|
||||
texts_.clear();
|
||||
texts_.push_back({"", stringToColor(options.palette, "white")});
|
||||
texts_.push_back({"INSTRUCTIONS:", stringToColor(options.palette, "yellow")});
|
||||
texts_.push_back({"", stringToColor(options.palette, "white")});
|
||||
texts_.push_back({"HELP JAILDOC TO GET BACK ALL", stringToColor(options.palette, "white")});
|
||||
texts_.push_back({"HIS PROJECTS AND GO TO THE", stringToColor(options.palette, "white")});
|
||||
texts_.push_back({"JAIL TO FINISH THEM", stringToColor(options.palette, "white")});
|
||||
texts_.push_back({"", stringToColor(options.palette, "white")});
|
||||
texts_.push_back({"", stringToColor(options.palette, "white")});
|
||||
|
||||
texts_.push_back({"KEYS:", stringToColor(options.palette, "yellow")});
|
||||
texts_.push_back({"", stringToColor(options.palette, "white")});
|
||||
texts_.push_back({keys + " TO MOVE AND JUMP", stringToColor(options.palette, "white")});
|
||||
texts_.push_back({"M TO SWITCH THE MUSIC", stringToColor(options.palette, "white")});
|
||||
texts_.push_back({"H TO PAUSE THE GAME", stringToColor(options.palette, "white")});
|
||||
texts_.push_back({"F1-F2 TO CHANGE WINDOWS SIZE", stringToColor(options.palette, "white")});
|
||||
texts_.push_back({"F3 TO SWITCH TO FULLSCREEN", stringToColor(options.palette, "white")});
|
||||
texts_.push_back({"B TO TOOGLE THE BORDER SCREEN", stringToColor(options.palette, "white")});
|
||||
texts_.push_back({"", stringToColor(options.palette, "white")});
|
||||
texts_.push_back({"", stringToColor(options.palette, "white")});
|
||||
|
||||
texts_.push_back({"A GAME BY JAILDESIGNER", stringToColor(options.palette, "yellow")});
|
||||
texts_.push_back({"MADE ON SUMMER/FALL 2022", stringToColor(options.palette, "yellow")});
|
||||
texts_.push_back({"", stringToColor(options.palette, "white")});
|
||||
texts_.push_back({"", stringToColor(options.palette, "white")});
|
||||
|
||||
texts_.push_back({"I LOVE JAILGAMES! ", stringToColor(options.palette, "white")});
|
||||
texts_.push_back({"", stringToColor(options.palette, "white")});
|
||||
#else
|
||||
texts.clear();
|
||||
texts.push_back({"", stringToColor(options.palette, "white")});
|
||||
texts.push_back({"INSTRUCTIONS:", stringToColor(options.palette, "yellow")});
|
||||
texts.push_back({"", stringToColor(options.palette, "white")});
|
||||
texts.push_back({"HELP JAILDOC TO GET BACK ALL", stringToColor(options.palette, "white")});
|
||||
texts.push_back({"HIS PROJECTS AND GO TO THE", stringToColor(options.palette, "white")});
|
||||
texts.push_back({"JAIL TO FINISH THEM", stringToColor(options.palette, "white")});
|
||||
texts.push_back({"", stringToColor(options.palette, "white")});
|
||||
texts.push_back({"", stringToColor(options.palette, "white")});
|
||||
|
||||
texts.push_back({"KEYS:", stringToColor(options.palette, "yellow")});
|
||||
texts.push_back({"", stringToColor(options.palette, "white")});
|
||||
texts.push_back({"B TO JUMP", stringToColor(options.palette, "white")});
|
||||
texts.push_back({"R TO SWITCH THE MUSIC", stringToColor(options.palette, "white")});
|
||||
texts.push_back({"L TO SWAP THE COLOR PALETTE", stringToColor(options.palette, "white")});
|
||||
texts.push_back({"START TO PAUSE", stringToColor(options.palette, "white")});
|
||||
texts.push_back({"SELECT TO EXIT", stringToColor(options.palette, "white")});
|
||||
texts.push_back({"", stringToColor(options.palette, "white")});
|
||||
texts.push_back({"", stringToColor(options.palette, "white")});
|
||||
texts.push_back({"", stringToColor(options.palette, "white")});
|
||||
|
||||
texts.push_back({"A GAME BY JAILDESIGNER", stringToColor(options.palette, "yellow")});
|
||||
texts.push_back({"MADE ON SUMMER/FALL 2022", stringToColor(options.palette, "yellow")});
|
||||
texts.push_back({"", stringToColor(options.palette, "white")});
|
||||
texts.push_back({"", stringToColor(options.palette, "white")});
|
||||
|
||||
texts.push_back({"I LOVE JAILGAMES! ", stringToColor(options.palette, "white")});
|
||||
texts.push_back({"", stringToColor(options.palette, "white")});
|
||||
#endif
|
||||
}
|
||||
|
||||
// Escribe el texto en la textura
|
||||
void Credits::fillTexture()
|
||||
{
|
||||
// Inicializa los textos
|
||||
iniTexts();
|
||||
|
||||
// Rellena la textura de texto
|
||||
SDL_SetRenderTarget(renderer_, text_texture_);
|
||||
color_t c = stringToColor(options.palette, "black");
|
||||
SDL_SetRenderDrawColor(renderer_, c.r, c.g, c.b, 0xFF);
|
||||
SDL_RenderClear(renderer_);
|
||||
|
||||
// Escribe el texto en la textura
|
||||
const int size = text_->getCharacterSize();
|
||||
int i = 0;
|
||||
|
||||
for (auto t : texts_)
|
||||
{
|
||||
text_->writeDX(TXT_CENTER | TXT_COLOR, PLAY_AREA_CENTER_X, i * size, t.label, 1, t.color);
|
||||
i++;
|
||||
}
|
||||
|
||||
// Escribe el corazón
|
||||
const int textLenght = text_->lenght(texts_[22].label, 1) - text_->lenght(" ", 1); // Se resta el ultimo caracter que es un espacio
|
||||
const int posX = ((PLAY_AREA_WIDTH - textLenght) / 2) + textLenght;
|
||||
text_->writeColored(posX, 176, "}", stringToColor(options.palette, "bright_red"));
|
||||
|
||||
// Recoloca el sprite del brillo
|
||||
sprite_->setPosX(posX + 2);
|
||||
|
||||
SDL_SetRenderTarget(renderer_, nullptr);
|
||||
|
||||
// Rellena la textura que cubre el texto con color transparente
|
||||
SDL_SetRenderTarget(renderer_, cover_texture_);
|
||||
SDL_SetRenderDrawColor(renderer_, c.r, c.g, c.b, 0x00);
|
||||
SDL_RenderClear(renderer_);
|
||||
|
||||
// Los primeros 8 pixels crea una malla
|
||||
SDL_SetRenderDrawColor(renderer_, c.r, c.g, c.b, 0xFF);
|
||||
for (int i = 0; i < 256; i += 2)
|
||||
{
|
||||
SDL_RenderDrawPoint(renderer_, i, 0);
|
||||
SDL_RenderDrawPoint(renderer_, i, 2);
|
||||
SDL_RenderDrawPoint(renderer_, i, 4);
|
||||
SDL_RenderDrawPoint(renderer_, i, 6);
|
||||
|
||||
SDL_RenderDrawPoint(renderer_, i + 1, 5);
|
||||
SDL_RenderDrawPoint(renderer_, i + 1, 7);
|
||||
}
|
||||
|
||||
// El resto se rellena de color sólido
|
||||
SDL_Rect rect = {0, 8, 256, 192};
|
||||
SDL_RenderFillRect(renderer_, &rect);
|
||||
|
||||
SDL_SetRenderTarget(renderer_, nullptr);
|
||||
}
|
||||
|
||||
// Actualiza el contador
|
||||
void Credits::updateCounter()
|
||||
{
|
||||
// Incrementa el contador
|
||||
if (counter_enabled_)
|
||||
{
|
||||
counter_++;
|
||||
if (counter_ == 224 || counter_ == 544 || counter_ == 672)
|
||||
{
|
||||
counter_enabled_ = false;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
sub_counter_++;
|
||||
if (sub_counter_ == 100)
|
||||
{
|
||||
counter_enabled_ = true;
|
||||
sub_counter_ = 0;
|
||||
}
|
||||
}
|
||||
|
||||
// Comprueba si ha terminado la sección
|
||||
if (counter_ > 1200)
|
||||
{
|
||||
options.section.name = SECTION_DEMO;
|
||||
}
|
||||
}
|
||||
|
||||
// Actualiza las variables
|
||||
void Credits::update()
|
||||
{
|
||||
// Comprueba que la diferencia de ticks sea mayor a la velocidad del juego
|
||||
if (SDL_GetTicks() - ticks_ > ticks_speed_)
|
||||
{
|
||||
// Actualiza el contador de ticks
|
||||
ticks_ = SDL_GetTicks();
|
||||
|
||||
// Comprueba las entradas
|
||||
checkInput();
|
||||
|
||||
// Actualiza el contador
|
||||
updateCounter();
|
||||
|
||||
screen_->update();
|
||||
|
||||
// Actualiza el sprite con el brillo
|
||||
if (counter_ > 770)
|
||||
{
|
||||
sprite_->update();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Dibuja en pantalla
|
||||
void Credits::render()
|
||||
{
|
||||
// Prepara para empezar a dibujar en la textura de juego
|
||||
screen_->start();
|
||||
|
||||
// Limpia la pantalla
|
||||
screen_->clean();
|
||||
|
||||
if (counter_ < 1150)
|
||||
{
|
||||
// Dibuja la textura con el texto en pantalla
|
||||
SDL_RenderCopy(renderer_, text_texture_, nullptr, nullptr);
|
||||
|
||||
// Dibuja la textura que cubre el texto
|
||||
const int offset = std::min(counter_ / 8, 192 / 2);
|
||||
SDL_Rect srcRect = {0, 0, 256, 192 - (offset * 2)};
|
||||
SDL_Rect dstRect = {0, offset * 2, 256, 192 - (offset * 2)};
|
||||
SDL_RenderCopy(renderer_, cover_texture_, &srcRect, &dstRect);
|
||||
|
||||
// Dibuja el sprite con el brillo
|
||||
sprite_->render();
|
||||
}
|
||||
|
||||
// Vuelca el contenido del renderizador en pantalla
|
||||
screen_->render();
|
||||
}
|
||||
|
||||
// Bucle para el logo del juego
|
||||
void Credits::run()
|
||||
{
|
||||
while (options.section.name == SECTION_CREDITS)
|
||||
{
|
||||
update();
|
||||
checkEvents();
|
||||
render();
|
||||
}
|
||||
}
|
||||
|
||||
// Cambia la paleta
|
||||
void Credits::switchPalette()
|
||||
{
|
||||
options.palette = options.palette == p_zxspectrum ? p_zxarne : p_zxspectrum;
|
||||
fillTexture();
|
||||
}
|
||||
77
source/credits.h
Normal file
77
source/credits.h
Normal file
@@ -0,0 +1,77 @@
|
||||
#pragma once
|
||||
|
||||
#include <SDL2/SDL_events.h> // Para SDL_Event
|
||||
#include <SDL2/SDL_render.h> // Para SDL_Renderer, SDL_Texture
|
||||
#include <SDL2/SDL_stdinc.h> // Para Uint32
|
||||
#include <string> // Para basic_string, string
|
||||
#include <vector> // Para vector
|
||||
#include "utils.h" // Para color_t
|
||||
class AnimatedSprite;
|
||||
class Asset;
|
||||
class Input;
|
||||
class Resource;
|
||||
class Screen;
|
||||
class Text;
|
||||
|
||||
class Credits
|
||||
{
|
||||
private:
|
||||
struct captions_t
|
||||
{
|
||||
std::string label; // Texto a escribir
|
||||
color_t color; // Color del texto
|
||||
};
|
||||
|
||||
// Objetos y punteros
|
||||
Screen *screen_; // Objeto encargado de dibujar en pantalla
|
||||
SDL_Renderer *renderer_; // El renderizador de la ventana
|
||||
Resource *resource_; // Objeto con los recursos
|
||||
Asset *asset_; // Objeto con los ficheros de recursos
|
||||
Input *input_; // Objeto pata gestionar la entrada
|
||||
Text *text_; // Objeto para escribir texto en pantalla
|
||||
SDL_Texture *text_texture_; // Textura para dibujar el texto
|
||||
SDL_Texture *cover_texture_; // Textura para cubrir el texto
|
||||
AnimatedSprite *sprite_; // Sprite para el brillo del corazón
|
||||
|
||||
// Variables
|
||||
int counter_ = 0; // Contador
|
||||
bool counter_enabled_ = true; // Indica si esta activo el contador
|
||||
int sub_counter_ = 0; // Contador secundario
|
||||
Uint32 ticks_ = 0; // Contador de ticks para ajustar la velocidad del programa
|
||||
Uint32 ticks_speed_ = 15; // Velocidad a la que se repiten los bucles del programa
|
||||
std::vector<captions_t> texts_; // Vector con los textos
|
||||
|
||||
// Actualiza las variables
|
||||
void update();
|
||||
|
||||
// Dibuja en pantalla
|
||||
void render();
|
||||
|
||||
// Comprueba el manejador de eventos
|
||||
void checkEvents();
|
||||
|
||||
// Comprueba las entradas
|
||||
void checkInput();
|
||||
|
||||
// Actualiza el contador
|
||||
void updateCounter();
|
||||
|
||||
// Inicializa los textos
|
||||
void iniTexts();
|
||||
|
||||
// Escribe el texto en la textura
|
||||
void fillTexture();
|
||||
|
||||
// Cambia la paleta
|
||||
void switchPalette();
|
||||
|
||||
public:
|
||||
// Constructor
|
||||
Credits();
|
||||
|
||||
// Destructor
|
||||
~Credits();
|
||||
|
||||
// Bucle principal
|
||||
void run();
|
||||
};
|
||||
128
source/debug.cpp
Normal file
128
source/debug.cpp
Normal file
@@ -0,0 +1,128 @@
|
||||
#include "debug.h"
|
||||
#include <algorithm> // Para max
|
||||
#include "asset.h" // Para Asset
|
||||
#include "text.h" // Para Text
|
||||
#include "texture.h" // Para Texture
|
||||
#include "utils.h"
|
||||
#include "screen.h"
|
||||
#include "asset.h"
|
||||
|
||||
// [SINGLETON]
|
||||
Debug *Debug::debug_ = nullptr;
|
||||
|
||||
// [SINGLETON] Crearemos el objeto con esta función estática
|
||||
void Debug::init()
|
||||
{
|
||||
Debug::debug_ = new Debug();
|
||||
}
|
||||
|
||||
// [SINGLETON] Destruiremos el objeto con esta función estática
|
||||
void Debug::destroy()
|
||||
{
|
||||
delete Debug::debug_;
|
||||
}
|
||||
|
||||
// [SINGLETON] Con este método obtenemos el objeto y podemos trabajar con él
|
||||
Debug *Debug::get()
|
||||
{
|
||||
return Debug::debug_;
|
||||
}
|
||||
|
||||
// Constructor
|
||||
Debug::Debug()
|
||||
// Copia la dirección de los objetos
|
||||
: screen_(Screen::get()),
|
||||
renderer_(Screen::get()->getRenderer()),
|
||||
asset_(Asset::get())
|
||||
{
|
||||
// Reserva memoria para los punteros
|
||||
texture_ = new Texture(renderer_, asset_->get("debug.png"));
|
||||
text_ = new Text(asset_->get("debug.txt"), texture_, renderer_);
|
||||
}
|
||||
|
||||
// Destructor
|
||||
Debug::~Debug()
|
||||
{
|
||||
delete texture_;
|
||||
delete text_;
|
||||
}
|
||||
|
||||
// Actualiza las variables
|
||||
void Debug::update()
|
||||
{
|
||||
}
|
||||
|
||||
// Dibuja en pantalla
|
||||
void Debug::render()
|
||||
{
|
||||
int y = y_;
|
||||
int w = 0;
|
||||
|
||||
for (auto s : slot_)
|
||||
{
|
||||
text_->write(x_, y, s);
|
||||
w = (std::max(w, (int)s.length()));
|
||||
y += text_->getCharacterSize() + 1;
|
||||
if (y > 192 - text_->getCharacterSize())
|
||||
{
|
||||
y = y_;
|
||||
x_ += w * text_->getCharacterSize() + 2;
|
||||
}
|
||||
}
|
||||
|
||||
y = 0;
|
||||
for (auto l : log_)
|
||||
{
|
||||
text_->writeColored(x_ + 10, y, l, color_t(255, 255, 255));
|
||||
y += text_->getCharacterSize() + 1;
|
||||
}
|
||||
}
|
||||
|
||||
// Establece la posición donde se colocará la información de debug
|
||||
void Debug::setPos(SDL_Point p)
|
||||
{
|
||||
x_ = p.x;
|
||||
y_ = p.y;
|
||||
}
|
||||
|
||||
// Añade un texto para mostrar
|
||||
void Debug::add(std::string text)
|
||||
{
|
||||
slot_.push_back(text);
|
||||
}
|
||||
|
||||
// Borra la información de debug
|
||||
void Debug::clear()
|
||||
{
|
||||
slot_.clear();
|
||||
}
|
||||
|
||||
// Añade un texto para mostrar en el apartado log
|
||||
void Debug::addToLog(std::string text)
|
||||
{
|
||||
log_.push_back(text);
|
||||
}
|
||||
|
||||
// Borra la información de debug del apartado log
|
||||
void Debug::clearLog()
|
||||
{
|
||||
log_.clear();
|
||||
}
|
||||
|
||||
// Establece el valor de la variable
|
||||
void Debug::setEnabled(bool value)
|
||||
{
|
||||
enabled_ = value;
|
||||
}
|
||||
|
||||
// Obtiene el valor de la variable
|
||||
bool Debug::getEnabled()
|
||||
{
|
||||
return enabled_;
|
||||
}
|
||||
|
||||
// Cambia el valor de la variable
|
||||
void Debug::switchEnabled()
|
||||
{
|
||||
enabled_ = !enabled_;
|
||||
}
|
||||
78
source/debug.h
Normal file
78
source/debug.h
Normal file
@@ -0,0 +1,78 @@
|
||||
#pragma once
|
||||
|
||||
#include <SDL2/SDL_rect.h> // Para SDL_Point
|
||||
#include <SDL2/SDL_render.h> // Para SDL_Renderer
|
||||
#include <string> // Para string
|
||||
#include <vector> // Para vector
|
||||
class Asset;
|
||||
class Screen;
|
||||
class Text;
|
||||
class Texture;
|
||||
|
||||
// Clase Debug
|
||||
class Debug
|
||||
{
|
||||
private:
|
||||
// [SINGLETON] Objeto privado
|
||||
static Debug *debug_;
|
||||
|
||||
// Objetos y punteros
|
||||
Screen *screen_; // Objeto encargado de dibujar en pantalla
|
||||
SDL_Renderer *renderer_; // El renderizador de la ventana
|
||||
Asset *asset_; // Objeto con los ficheros de recursos
|
||||
Text *text_; // Objeto encargado de escribir texto en pantalla
|
||||
Texture *texture_; // Textura para el texto
|
||||
|
||||
// Variables
|
||||
std::vector<std::string> slot_; // Vector con los textos a escribir
|
||||
std::vector<std::string> log_; // Vector con los textos a escribir
|
||||
int x_ = 0; // Posicion donde escribir el texto de debug
|
||||
int y_ = 0; // Posición donde escribir el texto de debug
|
||||
bool enabled_ = false; // Indica si esta activo el modo debug
|
||||
|
||||
// Constructor
|
||||
Debug();
|
||||
|
||||
// Destructor
|
||||
~Debug();
|
||||
|
||||
public:
|
||||
// [SINGLETON] Crearemos el objeto con esta función estática
|
||||
static void init();
|
||||
|
||||
// [SINGLETON] Destruiremos el objeto con esta función estática
|
||||
static void destroy();
|
||||
|
||||
// [SINGLETON] Con este método obtenemos el objeto y podemos trabajar con él
|
||||
static Debug *get();
|
||||
|
||||
// Actualiza las variables
|
||||
void update();
|
||||
|
||||
// Dibuja en pantalla
|
||||
void render();
|
||||
|
||||
// Establece la posición donde se colocará la información de debug
|
||||
void setPos(SDL_Point p);
|
||||
|
||||
// Añade un texto para mostrar
|
||||
void add(std::string text);
|
||||
|
||||
// Borra la información de debug
|
||||
void clear();
|
||||
|
||||
// Añade un texto para mostrar en el apartado log
|
||||
void addToLog(std::string text);
|
||||
|
||||
// Borra la información de debug del apartado log
|
||||
void clearLog();
|
||||
|
||||
// Establece el valor de la variable
|
||||
void setEnabled(bool value);
|
||||
|
||||
// Obtiene el valor de la variable
|
||||
bool getEnabled();
|
||||
|
||||
// Cambia el valor de la variable
|
||||
void switchEnabled();
|
||||
};
|
||||
@@ -1,7 +1,27 @@
|
||||
#include "gamestate_demo.h"
|
||||
#include "demo.h"
|
||||
#include <SDL2/SDL_rect.h> // Para SDL_Rect
|
||||
#include <SDL2/SDL_timer.h> // Para SDL_GetTicks
|
||||
#include <iostream> // Para basic_ostream, basic_ios, operator<<, cout
|
||||
#include "asset.h" // Para Asset
|
||||
#include "const.h" // Para BLOCK, PLAY_AREA_WIDTH, SECTION_DEMO
|
||||
#include "input.h" // Para Input, REPEAT_FALSE, inputs_e
|
||||
#include "item_tracker.h" // Para ItemTracker
|
||||
#include "resource.h" // Para Resource
|
||||
#include "room.h" // Para Room
|
||||
#include "screen.h" // Para Screen
|
||||
#include "text.h" // Para Text, TXT_CENTER, TXT_COLOR
|
||||
#include "utils.h" // Para color_t, stringToColor, options_t, secti...
|
||||
#include "options.h"
|
||||
#include "debug.h"
|
||||
|
||||
// Constructor
|
||||
Demo::Demo(SDL_Renderer *renderer, Screen *screen, Resource *resource, Asset *asset, Input *input, options_t *options, section_t *section, Debug *debug)
|
||||
Demo::Demo()
|
||||
: screen(Screen::get()),
|
||||
renderer(Screen::get()->getRenderer()),
|
||||
resource(Resource::get()),
|
||||
asset(Asset::get()),
|
||||
input(Input::get()),
|
||||
debug(Debug::get())
|
||||
{
|
||||
// Inicia algunas variables
|
||||
board.iniClock = SDL_GetTicks();
|
||||
@@ -17,21 +37,10 @@ Demo::Demo(SDL_Renderer *renderer, Screen *screen, Resource *resource, Asset *as
|
||||
roomIndex = 0;
|
||||
currentRoom = rooms[roomIndex];
|
||||
|
||||
// Copia los punteros
|
||||
this->renderer = renderer;
|
||||
this->screen = screen;
|
||||
this->resource = resource;
|
||||
this->asset = asset;
|
||||
this->input = input;
|
||||
this->options = options;
|
||||
this->section = section;
|
||||
this->debug = debug;
|
||||
|
||||
// Crea los objetos
|
||||
itemTracker = new ItemTracker();
|
||||
scoreboard = new ScoreBoard(renderer, resource, asset, options, &board);
|
||||
room = new Room(resource->getRoom(currentRoom), renderer, screen, asset, options, itemTracker, &board.items, false, debug);
|
||||
eventHandler = new SDL_Event();
|
||||
scoreboard = new Scoreboard(&board);
|
||||
room = new Room(resource->getRoom(currentRoom), itemTracker, &board.items, false);
|
||||
text = new Text(resource->getOffset("smb2.txt"), resource->getTexture("smb2.png"), renderer);
|
||||
|
||||
// Inicializa el resto de variables
|
||||
@@ -46,8 +55,8 @@ Demo::Demo(SDL_Renderer *renderer, Screen *screen, Resource *resource, Asset *as
|
||||
board.music = true;
|
||||
setScoreBoardColor();
|
||||
|
||||
section->name = SECTION_DEMO;
|
||||
section->subsection = 0;
|
||||
options.section.name = SECTION_DEMO;
|
||||
options.section.subsection = 0;
|
||||
}
|
||||
|
||||
Demo::~Demo()
|
||||
@@ -56,21 +65,20 @@ Demo::~Demo()
|
||||
delete itemTracker;
|
||||
delete scoreboard;
|
||||
delete room;
|
||||
delete eventHandler;
|
||||
delete text;
|
||||
}
|
||||
|
||||
// Comprueba los eventos de la cola
|
||||
void Demo::checkEvents()
|
||||
{
|
||||
// Comprueba los eventos que hay en la cola
|
||||
while (SDL_PollEvent(eventHandler) != 0)
|
||||
SDL_Event event;
|
||||
while (SDL_PollEvent(&event))
|
||||
{
|
||||
// Evento de salida de la aplicación
|
||||
if (eventHandler->type == SDL_QUIT)
|
||||
if (event.type == SDL_QUIT)
|
||||
{
|
||||
section->name = SECTION_QUIT;
|
||||
screen->setBorderColor(stringToColor(options->palette, "black"));
|
||||
options.section.name = SECTION_QUIT;
|
||||
screen->setBorderColor(stringToColor(options.palette, "black"));
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -79,21 +87,20 @@ void Demo::checkEvents()
|
||||
// Comprueba las entradas
|
||||
void Demo::checkInput()
|
||||
{
|
||||
|
||||
if (input->checkInput(input_exit, REPEAT_FALSE))
|
||||
{
|
||||
section->name = SECTION_QUIT;
|
||||
options.section.name = SECTION_QUIT;
|
||||
}
|
||||
|
||||
else if (input->checkInput(input_toggle_border, REPEAT_FALSE))
|
||||
{
|
||||
screen->switchBorder();
|
||||
screen->toggleBorder();
|
||||
reLoadTextures();
|
||||
}
|
||||
|
||||
else if (input->checkInput(input_window_fullscreen, REPEAT_FALSE))
|
||||
else if (input->checkInput(input_toggle_videomode, REPEAT_FALSE))
|
||||
{
|
||||
screen->switchVideoMode();
|
||||
screen->toggleVideoMode();
|
||||
reLoadTextures();
|
||||
}
|
||||
|
||||
@@ -109,22 +116,22 @@ void Demo::checkInput()
|
||||
reLoadTextures();
|
||||
}
|
||||
|
||||
else if (input->checkInput(input_swap_palette, REPEAT_FALSE))
|
||||
else if (input->checkInput(input_toggle_palette, REPEAT_FALSE))
|
||||
{
|
||||
switchPalette();
|
||||
}
|
||||
|
||||
else if (input->checkInput(input_pause, REPEAT_FALSE) || input->checkInput(input_accept, REPEAT_FALSE) || input->checkInput(input_jump, REPEAT_FALSE))
|
||||
{
|
||||
section->name = SECTION_TITLE;
|
||||
section->subsection = 0;
|
||||
options.section.name = SECTION_TITLE;
|
||||
options.section.subsection = 0;
|
||||
}
|
||||
}
|
||||
|
||||
// Bucle para el juego
|
||||
void Demo::run()
|
||||
{
|
||||
while (section->name == SECTION_DEMO)
|
||||
while (options.section.name == SECTION_DEMO)
|
||||
{
|
||||
update();
|
||||
checkEvents();
|
||||
@@ -150,8 +157,7 @@ void Demo::update()
|
||||
screen->updateFX();
|
||||
checkRoomChange();
|
||||
|
||||
// Actualiza las notificaciones
|
||||
screen->updateNotifier();
|
||||
screen->update();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -170,7 +176,7 @@ void Demo::render()
|
||||
screen->renderFX();
|
||||
|
||||
// Actualiza la pantalla
|
||||
screen->blit();
|
||||
screen->render();
|
||||
}
|
||||
|
||||
// Escribe el nombre de la pantalla
|
||||
@@ -178,7 +184,7 @@ void Demo::renderRoomName()
|
||||
{
|
||||
// Texto en el centro de la pantalla
|
||||
SDL_Rect rect = {0, 16 * BLOCK, PLAY_AREA_WIDTH, BLOCK * 2};
|
||||
color_t color = stringToColor(options->palette, "white");
|
||||
color_t color = stringToColor(options.palette, "white");
|
||||
SDL_SetRenderDrawColor(renderer, color.r, color.g, color.b, 0xFF);
|
||||
SDL_RenderFillRect(renderer, &rect);
|
||||
|
||||
@@ -188,7 +194,7 @@ void Demo::renderRoomName()
|
||||
// Recarga todas las texturas
|
||||
void Demo::reLoadTextures()
|
||||
{
|
||||
if (options->console)
|
||||
if (options.console)
|
||||
{
|
||||
std::cout << "** RELOAD REQUESTED" << std::endl;
|
||||
}
|
||||
@@ -201,13 +207,13 @@ void Demo::reLoadTextures()
|
||||
void Demo::switchPalette()
|
||||
{
|
||||
// Modifica la variable
|
||||
if (options->palette == p_zxspectrum)
|
||||
if (options.palette == p_zxspectrum)
|
||||
{
|
||||
options->palette = p_zxarne;
|
||||
options.palette = p_zxarne;
|
||||
}
|
||||
else
|
||||
{
|
||||
options->palette = p_zxspectrum;
|
||||
options.palette = p_zxspectrum;
|
||||
}
|
||||
|
||||
room->reLoadPalette();
|
||||
@@ -230,7 +236,7 @@ bool Demo::changeRoom(std::string file)
|
||||
room = nullptr;
|
||||
|
||||
// Crea un objeto habitación nuevo a partir del fichero
|
||||
room = new Room(resource->getRoom(file), renderer, screen, asset, options, itemTracker, &board.items, false, debug);
|
||||
room = new Room(resource->getRoom(file), itemTracker, &board.items, false);
|
||||
|
||||
// Pone el color del marcador en función del color del borde de la habitación
|
||||
setScoreBoardColor();
|
||||
@@ -251,8 +257,8 @@ void Demo::checkRoomChange()
|
||||
roomIndex++;
|
||||
if (roomIndex == (int)rooms.size())
|
||||
{
|
||||
section->name = SECTION_LOGO;
|
||||
section->subsection = SUBSECTION_LOGO_TO_TITLE;
|
||||
options.section.name = SECTION_LOGO;
|
||||
options.section.subsection = SUBSECTION_LOGO_TO_TITLE;
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -268,10 +274,10 @@ void Demo::setScoreBoardColor()
|
||||
const color_t c = room->getBorderColor();
|
||||
|
||||
// Si el color es negro lo cambia a blanco
|
||||
const color_t cBlack = stringToColor(options->palette, "black");
|
||||
board.color = colorAreEqual(c, cBlack) ? stringToColor(options->palette, "white") : c;
|
||||
const color_t cBlack = stringToColor(options.palette, "black");
|
||||
board.color = colorAreEqual(c, cBlack) ? stringToColor(options.palette, "white") : c;
|
||||
|
||||
// Si el color es negro brillante lo cambia a blanco
|
||||
const color_t cBrightBlack = stringToColor(options->palette, "bright_black");
|
||||
board.color = colorAreEqual(c, cBrightBlack) ? stringToColor(options->palette, "white") : c;
|
||||
const color_t cBrightBlack = stringToColor(options.palette, "bright_black");
|
||||
board.color = colorAreEqual(c, cBrightBlack) ? stringToColor(options.palette, "white") : c;
|
||||
}
|
||||
@@ -1,42 +1,36 @@
|
||||
#pragma once
|
||||
|
||||
#include <SDL2/SDL.h>
|
||||
#include "jail_engine/animatedsprite.h"
|
||||
#include "jail_engine/asset.h"
|
||||
#include "jail_engine/input.h"
|
||||
#include "jail_engine/debug.h"
|
||||
#include "jail_engine/input.h"
|
||||
#include "jail_engine/resource.h"
|
||||
#include "jail_engine/screen.h"
|
||||
#include "jail_engine/sprite.h"
|
||||
#include "jail_engine/text.h"
|
||||
#include "jail_engine/utils.h"
|
||||
#include "const.h"
|
||||
#include "item_tracker.h"
|
||||
#include "room_tracker.h"
|
||||
#include "room.h"
|
||||
#include "scoreboard.h"
|
||||
|
||||
#ifndef DEMO_H
|
||||
#define DEMO_H
|
||||
#include <SDL2/SDL_events.h> // Para SDL_Event
|
||||
#include <SDL2/SDL_render.h> // Para SDL_Renderer
|
||||
#include <SDL2/SDL_stdinc.h> // Para Uint32
|
||||
#include <string> // Para string, basic_string
|
||||
#include <vector> // Para vector
|
||||
#include "scoreboard.h" // Para board_t
|
||||
class Asset;
|
||||
class Debug;
|
||||
class Input;
|
||||
class ItemTracker;
|
||||
class Resource;
|
||||
class Room;
|
||||
class Screen;
|
||||
class Text;
|
||||
struct options_t;
|
||||
struct section_t;
|
||||
|
||||
class Demo
|
||||
{
|
||||
private:
|
||||
// Objetos y punteros
|
||||
SDL_Renderer *renderer; // El renderizador de la ventana
|
||||
SDL_Event *eventHandler; // Manejador de eventos
|
||||
Screen *screen; // Objeto encargado de manejar el renderizador
|
||||
SDL_Renderer *renderer; // El renderizador de la ventana
|
||||
Room *room; // Objeto encargado de gestionar cada habitación del juego
|
||||
Resource *resource; // Objeto con los recursos
|
||||
Asset *asset; // Objeto con la ruta a todos los ficheros de recursos
|
||||
Input *input; // Objeto pata gestionar la entrada
|
||||
Text *text; // Objeto para los textos del juego
|
||||
ScoreBoard *scoreboard; // Objeto encargado de gestionar el marcador
|
||||
Scoreboard *scoreboard; // Objeto encargado de gestionar el marcador
|
||||
ItemTracker *itemTracker; // Lleva el control de los objetos recogidos
|
||||
Debug *debug; // Objeto para gestionar la información de debug
|
||||
options_t *options; // Puntero a las opciones del juego
|
||||
section_t *section; // Seccion actual dentro del juego
|
||||
|
||||
// Variables
|
||||
Uint32 ticks; // Contador de ticks para ajustar la velocidad del programa
|
||||
@@ -80,7 +74,7 @@ private:
|
||||
|
||||
public:
|
||||
// Constructor
|
||||
Demo(SDL_Renderer *renderer, Screen *screen, Resource *resource, Asset *asset, Input *input, options_t *options, section_t *section, Debug *debug);
|
||||
Demo();
|
||||
|
||||
// Destructor
|
||||
~Demo();
|
||||
@@ -88,5 +82,3 @@ public:
|
||||
// Bucle para el juego
|
||||
void run();
|
||||
};
|
||||
|
||||
#endif
|
||||
1553
source/director.cpp
1553
source/director.cpp
File diff suppressed because it is too large
Load Diff
@@ -1,82 +1,37 @@
|
||||
#pragma once
|
||||
#include <SDL2/SDL.h>
|
||||
#include "jail_engine/asset.h"
|
||||
#include "jail_engine/debug.h"
|
||||
#include "jail_engine/input.h"
|
||||
#include "jail_engine/jail_audio.h"
|
||||
#include "jail_engine/movingsprite.h"
|
||||
#include "jail_engine/resource.h"
|
||||
#include "jail_engine/sprite.h"
|
||||
#include "jail_engine/text.h"
|
||||
#include "jail_engine/utils.h"
|
||||
#include "const.h"
|
||||
#include "gamestate_credits.h"
|
||||
#include "gamestate_demo.h"
|
||||
#include "gamestate_ending.h"
|
||||
#include "gamestate_ending2.h"
|
||||
#include "enter_id.h"
|
||||
#include "gamestate_game_over.h"
|
||||
#include "gamestate_game.h"
|
||||
#include "gamestate_loading_screen.h"
|
||||
#include "gamestate_logo.h"
|
||||
#include "online.h"
|
||||
#include "gamestate_title.h"
|
||||
|
||||
#ifndef DIRECTOR_H
|
||||
#define DIRECTOR_H
|
||||
#include <SDL2/SDL_render.h> // for SDL_Renderer
|
||||
#include <SDL2/SDL_video.h> // for SDL_Window
|
||||
#include <string> // for string
|
||||
class Asset; // lines 6-6
|
||||
class Debug; // lines 8-8
|
||||
class Input; // lines 14-14
|
||||
class Resource; // lines 17-17
|
||||
class Screen; // lines 18-18
|
||||
struct JA_Music_t; // lines 20-20
|
||||
struct options_t; // lines 21-21
|
||||
struct section_t; // lines 22-22
|
||||
|
||||
class Director
|
||||
{
|
||||
private:
|
||||
// Objetos y punteros
|
||||
SDL_Window *window; // La ventana donde dibujamos
|
||||
SDL_Renderer *renderer; // El renderizador de la ventana
|
||||
Screen *screen; // Objeto encargado de dibujar en pantalla
|
||||
Resource *resource; // Objeto con los recursos
|
||||
Asset *asset; // Objeto que gestiona todos los ficheros de recursos
|
||||
Input *input; // Objeto Input para gestionar las entradas
|
||||
Game *game; // Objeto para gestionar la sección del juego
|
||||
Logo *logo; // Objeto para gestionar la sección del logo del programa
|
||||
Title *title; // Objeto para gestionar la pantalla de título
|
||||
LoadingScreen *loadingScreen; // Objeto para gestionar la introducción del juego
|
||||
Credits *credits; // Objeto para gestionar los creditos del juego
|
||||
Demo *demo; // Objeto para gestionar el modo demo, en el que se ven pantallas del juego
|
||||
Ending *ending; // Objeto para gestionar el final del juego
|
||||
Ending2 *ending2; // Objeto para gestionar el final del juego
|
||||
GameOver *gameOver; // Objeto para gestionar el final de la partida
|
||||
Debug *debug; // Objeto para getsionar la información de debug
|
||||
Online *online; // Objeto para gestionar la lectura y escritura de datos en el servidor remoto
|
||||
struct options_t *options; // Variable con todas las opciones del programa
|
||||
section_t *section; // Sección y subsección actual del programa;
|
||||
SDL_Window *window_; // La ventana donde dibujamos
|
||||
SDL_Renderer *renderer_; // El renderizador de la ventana
|
||||
|
||||
// Variables
|
||||
JA_Music_t *music; // Musica del titulo
|
||||
std::string executablePath; // Path del ejecutable
|
||||
std::string systemFolder; // Carpeta del sistema donde guardar datos
|
||||
|
||||
// Crea e inicializa las opciones del programa
|
||||
void initOptions();
|
||||
|
||||
// Inicializa los servicios online
|
||||
void initOnline();
|
||||
JA_Music_t *title_music_; // Musica del titulo
|
||||
std::string executable_path_; // Path del ejecutable
|
||||
std::string system_folder_; // Carpeta del sistema donde guardar datos
|
||||
|
||||
// Comprueba los parametros del programa
|
||||
void checkProgramArguments(int argc, char *argv[]);
|
||||
|
||||
// Carga el fichero de configuración
|
||||
bool loadConfig();
|
||||
|
||||
// Guarda el fichero de configuración
|
||||
bool saveConfig();
|
||||
std::string checkProgramArguments(int argc, const char *argv[]);
|
||||
|
||||
// Crea la carpeta del sistema donde guardar datos
|
||||
void createSystemFolder(std::string folder);
|
||||
void createSystemFolder(const std::string &folder);
|
||||
|
||||
// Carga los recursos
|
||||
void loadResources(section_t *section);
|
||||
|
||||
// Asigna variables a partir de dos cadenas
|
||||
bool setOptions(options_t *options, std::string var, std::string value);
|
||||
void loadResources(section_t section);
|
||||
|
||||
// Inicializa jail_audio
|
||||
void initJailAudio();
|
||||
@@ -119,13 +74,11 @@ private:
|
||||
|
||||
public:
|
||||
// Constructor
|
||||
Director(int argc, char *argv[]);
|
||||
Director(int argc, const char *argv[]);
|
||||
|
||||
// Destructor
|
||||
~Director();
|
||||
|
||||
// Bucle principal
|
||||
void run();
|
||||
};
|
||||
|
||||
#endif
|
||||
int run();
|
||||
};
|
||||
@@ -1,19 +1,32 @@
|
||||
#include "gamestate_ending.h"
|
||||
#include "ending.h"
|
||||
#include <SDL2/SDL_blendmode.h> // Para SDL_BLENDMODE_BLEND
|
||||
#include <SDL2/SDL_error.h> // Para SDL_GetError
|
||||
#include <SDL2/SDL_pixels.h> // Para SDL_PIXELFORMAT_RGBA8888
|
||||
#include <SDL2/SDL_rect.h> // Para SDL_Rect
|
||||
#include <SDL2/SDL_timer.h> // Para SDL_GetTicks
|
||||
#include <algorithm> // Para min
|
||||
#include <iostream> // Para basic_ostream, operator<<, basic_ios
|
||||
#include "asset.h" // Para Asset
|
||||
#include "const.h" // Para GAMECANVAS_HEIGHT, GAMECANVAS_WIDTH
|
||||
#include "input.h" // Para Input, REPEAT_FALSE, inputs_e
|
||||
#include "jail_audio.h" // Para JA_SetVolume, JA_DeleteMusic, JA_Loa...
|
||||
#include "resource.h" // Para Resource
|
||||
#include "screen.h" // Para Screen
|
||||
#include "sprite.h" // Para Sprite
|
||||
#include "text.h" // Para Text, TXT_STROKE
|
||||
#include "texture.h" // Para Texture
|
||||
#include "utils.h" // Para color_t, stringToColor, options_t
|
||||
#include "options.h"
|
||||
|
||||
// Constructor
|
||||
Ending::Ending(SDL_Renderer *renderer, Screen *screen, Resource *resource, Asset *asset, Input *input, options_t *options, section_t *section)
|
||||
Ending::Ending()
|
||||
: screen(Screen::get()),
|
||||
renderer(Screen::get()->getRenderer()),
|
||||
resource(Resource::get()),
|
||||
asset(Asset::get()),
|
||||
input(Input::get())
|
||||
{
|
||||
// Copia los punteros
|
||||
this->renderer = renderer;
|
||||
this->screen = screen;
|
||||
this->resource = resource;
|
||||
this->asset = asset;
|
||||
this->input = input;
|
||||
this->options = options;
|
||||
this->section = section;
|
||||
|
||||
// Reserva memoria para los punteros a objetos
|
||||
eventHandler = new SDL_Event();
|
||||
text = new Text(resource->getOffset("smb2.txt"), resource->getTexture("smb2.png"), renderer);
|
||||
music = JA_LoadMusic(asset->get("ending1.ogg").c_str());
|
||||
|
||||
@@ -21,8 +34,8 @@ Ending::Ending(SDL_Renderer *renderer, Screen *screen, Resource *resource, Asset
|
||||
counter = -1;
|
||||
preCounter = 0;
|
||||
coverCounter = 0;
|
||||
section->name = SECTION_ENDING;
|
||||
section->subsection = 0;
|
||||
options.section.name = SECTION_ENDING;
|
||||
options.section.subsection = 0;
|
||||
ticks = 0;
|
||||
ticksSpeed = 15;
|
||||
scene = 0;
|
||||
@@ -37,13 +50,13 @@ Ending::Ending(SDL_Renderer *renderer, Screen *screen, Resource *resource, Asset
|
||||
iniScenes();
|
||||
|
||||
// Cambia el color del borde
|
||||
screen->setBorderColor(stringToColor(options->palette, "black"));
|
||||
screen->setBorderColor(stringToColor(options.palette, "black"));
|
||||
|
||||
// Crea la textura para cubrir el rexto
|
||||
coverTexture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, GAMECANVAS_WIDTH, GAMECANVAS_HEIGHT + 8);
|
||||
if (coverTexture == nullptr)
|
||||
{
|
||||
if (options->console)
|
||||
if (options.console)
|
||||
{
|
||||
std::cout << "Error: canvasTexture could not be created!\nSDL Error: " << SDL_GetError() << std::endl;
|
||||
}
|
||||
@@ -58,7 +71,6 @@ Ending::Ending(SDL_Renderer *renderer, Screen *screen, Resource *resource, Asset
|
||||
Ending::~Ending()
|
||||
{
|
||||
// Libera la memoria de los objetos
|
||||
delete eventHandler;
|
||||
delete text;
|
||||
SDL_DestroyTexture(coverTexture);
|
||||
|
||||
@@ -106,8 +118,7 @@ void Ending::update()
|
||||
// Actualiza el volumen de la musica
|
||||
updateMusicVolume();
|
||||
|
||||
// Actualiza las notificaciones
|
||||
screen->updateNotifier();
|
||||
screen->update();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -118,7 +129,7 @@ void Ending::render()
|
||||
screen->start();
|
||||
|
||||
// Limpia la pantalla
|
||||
screen->clean(stringToColor(options->palette, "black"));
|
||||
screen->clean(stringToColor(options.palette, "black"));
|
||||
|
||||
// Dibuja las imagenes de la escena
|
||||
spritePics[scene].sprite->render();
|
||||
@@ -140,19 +151,19 @@ void Ending::render()
|
||||
// text->write(0, 0, std::to_string(counter));
|
||||
|
||||
// Vuelca el contenido del renderizador en pantalla
|
||||
screen->blit();
|
||||
screen->render();
|
||||
}
|
||||
|
||||
// Comprueba el manejador de eventos
|
||||
void Ending::checkEvents()
|
||||
{
|
||||
// Comprueba los eventos que hay en la cola
|
||||
while (SDL_PollEvent(eventHandler) != 0)
|
||||
SDL_Event event;
|
||||
while (SDL_PollEvent(&event))
|
||||
{
|
||||
// Evento de salida de la aplicación
|
||||
if (eventHandler->type == SDL_QUIT)
|
||||
if (event.type == SDL_QUIT)
|
||||
{
|
||||
section->name = SECTION_QUIT;
|
||||
options.section.name = SECTION_QUIT;
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -164,18 +175,18 @@ void Ending::checkInput()
|
||||
|
||||
if (input->checkInput(input_exit, REPEAT_FALSE))
|
||||
{
|
||||
section->name = SECTION_LOGO;
|
||||
section->subsection = SUBSECTION_LOGO_TO_INTRO;
|
||||
options.section.name = SECTION_LOGO;
|
||||
options.section.subsection = SUBSECTION_LOGO_TO_INTRO;
|
||||
}
|
||||
|
||||
else if (input->checkInput(input_toggle_border, REPEAT_FALSE))
|
||||
{
|
||||
screen->switchBorder();
|
||||
screen->toggleBorder();
|
||||
}
|
||||
|
||||
else if (input->checkInput(input_window_fullscreen, REPEAT_FALSE))
|
||||
else if (input->checkInput(input_toggle_videomode, REPEAT_FALSE))
|
||||
{
|
||||
screen->switchVideoMode();
|
||||
screen->toggleVideoMode();
|
||||
}
|
||||
|
||||
else if (input->checkInput(input_window_dec_size, REPEAT_FALSE))
|
||||
@@ -188,7 +199,7 @@ void Ending::checkInput()
|
||||
screen->incWindowSize();
|
||||
}
|
||||
|
||||
else if (input->checkInput(input_swap_palette, REPEAT_FALSE))
|
||||
else if (input->checkInput(input_toggle_palette, REPEAT_FALSE))
|
||||
{
|
||||
switchPalette();
|
||||
}
|
||||
@@ -244,7 +255,7 @@ void Ending::iniTexts()
|
||||
endingTexture_t st;
|
||||
const int width = text->lenght(t.caption, 1) + 2 + 2;
|
||||
const int height = text->getCharacterSize() + 2 + 2;
|
||||
color_t c = stringToColor(options->palette, "black");
|
||||
color_t c = stringToColor(options.palette, "black");
|
||||
|
||||
// Crea la texture
|
||||
st.texture = new Texture(renderer);
|
||||
@@ -268,7 +279,7 @@ void Ending::iniTexts()
|
||||
SDL_RenderClear(renderer);
|
||||
|
||||
// Los primeros 8 pixels crea una malla
|
||||
c = stringToColor(options->palette, "black");
|
||||
c = stringToColor(options.palette, "black");
|
||||
SDL_SetRenderDrawColor(renderer, c.r, c.g, c.b, 0xFF);
|
||||
for (int i = 0; i < width; i += 2)
|
||||
{
|
||||
@@ -283,7 +294,7 @@ void Ending::iniTexts()
|
||||
|
||||
// El resto se rellena de color sólido
|
||||
SDL_Rect rect = {0, 8, width, height};
|
||||
c = stringToColor(options->palette, "black");
|
||||
c = stringToColor(options.palette, "black");
|
||||
SDL_SetRenderDrawColor(renderer, c.r, c.g, c.b, 0xFF);
|
||||
SDL_RenderFillRect(renderer, &rect);
|
||||
|
||||
@@ -306,7 +317,7 @@ void Ending::iniPics()
|
||||
// Vector con las rutas y la posición
|
||||
std::vector<textAndPos_t> pics;
|
||||
|
||||
if (options->palette == p_zxspectrum)
|
||||
if (options.palette == p_zxspectrum)
|
||||
{
|
||||
pics.push_back({"ending1.png", 48});
|
||||
pics.push_back({"ending2.png", 26});
|
||||
@@ -356,7 +367,7 @@ void Ending::iniPics()
|
||||
SDL_RenderClear(renderer);
|
||||
|
||||
// Los primeros 8 pixels crea una malla
|
||||
color_t c = stringToColor(options->palette, "black");
|
||||
color_t c = stringToColor(options.palette, "black");
|
||||
SDL_SetRenderDrawColor(renderer, c.r, c.g, c.b, 0xFF);
|
||||
for (int i = 0; i < width; i += 2)
|
||||
{
|
||||
@@ -371,7 +382,7 @@ void Ending::iniPics()
|
||||
|
||||
// El resto se rellena de color sólido
|
||||
SDL_Rect rect = {0, 8, width, height};
|
||||
c = stringToColor(options->palette, "black");
|
||||
c = stringToColor(options.palette, "black");
|
||||
SDL_SetRenderDrawColor(renderer, c.r, c.g, c.b, 0xFF);
|
||||
SDL_RenderFillRect(renderer, &rect);
|
||||
|
||||
@@ -476,7 +487,7 @@ void Ending::run()
|
||||
{
|
||||
JA_PlayMusic(music);
|
||||
|
||||
while (section->name == SECTION_ENDING)
|
||||
while (options.section.name == SECTION_ENDING)
|
||||
{
|
||||
update();
|
||||
checkEvents();
|
||||
@@ -560,7 +571,7 @@ void Ending::checkChangeScene()
|
||||
if (scene == 5)
|
||||
{
|
||||
// Termina el bucle
|
||||
section->name = SECTION_ENDING2;
|
||||
options.section.name = SECTION_ENDING2;
|
||||
|
||||
// Mantiene los valores anteriores
|
||||
scene = 4;
|
||||
@@ -578,7 +589,7 @@ void Ending::fillCoverTexture()
|
||||
SDL_RenderClear(renderer);
|
||||
|
||||
// Los primeros 8 pixels crea una malla
|
||||
const color_t c = stringToColor(options->palette, "brack");
|
||||
const color_t c = stringToColor(options.palette, "brack");
|
||||
SDL_SetRenderDrawColor(renderer, c.r, c.g, c.b, 0xFF);
|
||||
for (int i = 0; i < 256; i += 2)
|
||||
{
|
||||
@@ -624,9 +635,9 @@ void Ending::updateMusicVolume()
|
||||
// Cambia la paleta
|
||||
void Ending::switchPalette()
|
||||
{
|
||||
if (options->palette == p_zxspectrum)
|
||||
if (options.palette == p_zxspectrum)
|
||||
{
|
||||
options->palette = p_zxarne;
|
||||
options.palette = p_zxarne;
|
||||
|
||||
spritePics[0].sprite->setTexture(resource->getTexture("ending1_zxarne.png"));
|
||||
spritePics[1].sprite->setTexture(resource->getTexture("ending2_zxarne.png"));
|
||||
@@ -636,7 +647,7 @@ void Ending::switchPalette()
|
||||
}
|
||||
else
|
||||
{
|
||||
options->palette = p_zxspectrum;
|
||||
options.palette = p_zxspectrum;
|
||||
|
||||
spritePics[0].sprite->setTexture(resource->getTexture("ending1.png"));
|
||||
spritePics[1].sprite->setTexture(resource->getTexture("ending2.png"));
|
||||
@@ -1,21 +1,20 @@
|
||||
#pragma once
|
||||
#include <SDL2/SDL.h>
|
||||
#include "jail_engine/asset.h"
|
||||
#include "jail_engine/input.h"
|
||||
#include "jail_engine/jail_audio.h"
|
||||
#include "jail_engine/resource.h"
|
||||
#include "jail_engine/screen.h"
|
||||
#include "jail_engine/sprite.h"
|
||||
#include "jail_engine/sprite.h"
|
||||
#include "jail_engine/text.h"
|
||||
#include "jail_engine/texture.h"
|
||||
#include "jail_engine/utils.h"
|
||||
#include "const.h"
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#ifndef ENDING_H
|
||||
#define ENDING_H
|
||||
#include <SDL2/SDL_events.h> // Para SDL_Event
|
||||
#include <SDL2/SDL_render.h> // Para SDL_Renderer, SDL_Texture
|
||||
#include <SDL2/SDL_stdinc.h> // Para Uint32
|
||||
#include <string> // Para basic_string, string
|
||||
#include <vector> // Para vector
|
||||
class Asset;
|
||||
class Input;
|
||||
class Resource;
|
||||
class Screen;
|
||||
class Sprite;
|
||||
class Text;
|
||||
class Texture;
|
||||
struct JA_Music_t;
|
||||
struct options_t;
|
||||
struct section_t;
|
||||
|
||||
class Ending
|
||||
{
|
||||
@@ -51,16 +50,13 @@ private:
|
||||
};
|
||||
|
||||
// Objetos y punteros
|
||||
SDL_Renderer *renderer; // El renderizador de la ventana
|
||||
Screen *screen; // Objeto encargado de dibujar en pantalla
|
||||
SDL_Renderer *renderer; // El renderizador de la ventana
|
||||
Resource *resource; // Objeto con los recursos
|
||||
Asset *asset; // Objeto con los ficheros de recursos
|
||||
Input *input; // Objeto pata gestionar la entrada
|
||||
options_t *options; // Puntero a las opciones del juego
|
||||
SDL_Event *eventHandler; // Manejador de eventos
|
||||
Text *text; // Objeto para escribir texto en pantalla
|
||||
SDL_Texture *coverTexture; // Textura para cubrir el texto
|
||||
section_t *section; // Estado del bucle principal para saber si continua o se sale
|
||||
|
||||
// Variables
|
||||
int counter; // Contador
|
||||
@@ -118,13 +114,11 @@ private:
|
||||
|
||||
public:
|
||||
// Constructor
|
||||
Ending(SDL_Renderer *renderer, Screen *screen, Resource *resource, Asset *asset, Input *input, options_t *options, section_t *section);
|
||||
Ending();
|
||||
|
||||
// Destructor
|
||||
~Ending();
|
||||
|
||||
// Bucle principal
|
||||
void run();
|
||||
};
|
||||
|
||||
#endif
|
||||
};
|
||||
@@ -1,20 +1,29 @@
|
||||
#include "gamestate_ending2.h"
|
||||
#include <algorithm>
|
||||
#include "ending2.h"
|
||||
#include <SDL2/SDL_blendmode.h> // for SDL_BLENDMODE_BLEND
|
||||
#include <SDL2/SDL_timer.h> // for SDL_GetTicks
|
||||
#include <algorithm> // for max, min, replace
|
||||
#include "animatedsprite.h" // for AnimatedSprite
|
||||
#include "asset.h" // for Asset
|
||||
#include "const.h" // for GAMECANVAS_HEIGHT, GAMECANVAS_CENTER_X
|
||||
#include "input.h" // for Input, REPEAT_FALSE, inputs_e
|
||||
#include "jail_audio.h" // for JA_SetVolume, JA_DeleteMusic, JA_Loa...
|
||||
#include "movingsprite.h" // for MovingSprite
|
||||
#include "resource.h" // for Resource
|
||||
#include "screen.h" // for Screen
|
||||
#include "text.h" // for Text
|
||||
#include "texture.h" // for Texture
|
||||
#include "utils.h" // for color_t, stringToColor, options_t
|
||||
#include "options.h"
|
||||
|
||||
// Constructor
|
||||
Ending2::Ending2(SDL_Renderer *renderer, Screen *screen, Resource *resource, Asset *asset, Input *input, options_t *options, section_t *section)
|
||||
Ending2::Ending2()
|
||||
: screen(Screen::get()),
|
||||
renderer(Screen::get()->getRenderer()),
|
||||
resource(Resource::get()),
|
||||
asset(Asset::get()),
|
||||
input(Input::get())
|
||||
{
|
||||
// Copia los punteros
|
||||
this->renderer = renderer;
|
||||
this->screen = screen;
|
||||
this->resource = resource;
|
||||
this->asset = asset;
|
||||
this->input = input;
|
||||
this->options = options;
|
||||
this->section = section;
|
||||
|
||||
// Reserva memoria para los punteros a objetos
|
||||
eventHandler = new SDL_Event();
|
||||
text = new Text(resource->getOffset("smb2.txt"), resource->getTexture("smb2.png"), renderer);
|
||||
music = JA_LoadMusic(asset->get("ending2.ogg").c_str());
|
||||
|
||||
@@ -23,8 +32,8 @@ Ending2::Ending2(SDL_Renderer *renderer, Screen *screen, Resource *resource, Ass
|
||||
preCounter = 0;
|
||||
postCounter = 0;
|
||||
postCounterEnabled = false;
|
||||
section->name = SECTION_ENDING2;
|
||||
section->subsection = 0;
|
||||
options.section.name = SECTION_ENDING2;
|
||||
options.section.subsection = 0;
|
||||
ticks = 0;
|
||||
ticksSpeed = 15;
|
||||
distSpriteText = 8;
|
||||
@@ -37,11 +46,11 @@ Ending2::Ending2(SDL_Renderer *renderer, Screen *screen, Resource *resource, Ass
|
||||
const std::vector<std::string> colorList = {"white", "yellow", "cyan", "green", "magenta", "red", "blue", "black"};
|
||||
for (auto cl : colorList)
|
||||
{
|
||||
colors.push_back(stringToColor(options->palette, cl));
|
||||
colors.push_back(stringToColor(options.palette, cl));
|
||||
}
|
||||
|
||||
// Cambia el color del borde
|
||||
screen->setBorderColor(stringToColor(options->palette, "black"));
|
||||
screen->setBorderColor(stringToColor(options.palette, "black"));
|
||||
|
||||
// Inicializa la lista de sprites
|
||||
iniSpriteList();
|
||||
@@ -63,7 +72,6 @@ Ending2::Ending2(SDL_Renderer *renderer, Screen *screen, Resource *resource, Ass
|
||||
Ending2::~Ending2()
|
||||
{
|
||||
// Libera la memoria de los objetos
|
||||
delete eventHandler;
|
||||
delete text;
|
||||
JA_DeleteMusic(music);
|
||||
|
||||
@@ -105,8 +113,7 @@ void Ending2::update()
|
||||
// Actualiza el volumen de la musica
|
||||
updateMusicVolume();
|
||||
|
||||
// Actualiza las notificaciones
|
||||
screen->updateNotifier();
|
||||
screen->update();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -117,7 +124,7 @@ void Ending2::render()
|
||||
screen->start();
|
||||
|
||||
// Limpia la pantalla
|
||||
screen->clean(stringToColor(options->palette, "black"));
|
||||
screen->clean(stringToColor(options.palette, "black"));
|
||||
|
||||
// Dibuja los sprites
|
||||
renderSprites();
|
||||
@@ -174,19 +181,19 @@ void Ending2::render()
|
||||
}
|
||||
|
||||
// Vuelca el contenido del renderizador en pantalla
|
||||
screen->blit();
|
||||
screen->render();
|
||||
}
|
||||
|
||||
// Comprueba el manejador de eventos
|
||||
void Ending2::checkEvents()
|
||||
{
|
||||
// Comprueba los eventos que hay en la cola
|
||||
while (SDL_PollEvent(eventHandler) != 0)
|
||||
SDL_Event event;
|
||||
while (SDL_PollEvent(&event))
|
||||
{
|
||||
// Evento de salida de la aplicación
|
||||
if (eventHandler->type == SDL_QUIT)
|
||||
if (event.type == SDL_QUIT)
|
||||
{
|
||||
section->name = SECTION_QUIT;
|
||||
options.section.name = SECTION_QUIT;
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -198,18 +205,18 @@ void Ending2::checkInput()
|
||||
|
||||
if (input->checkInput(input_exit, REPEAT_FALSE))
|
||||
{
|
||||
section->name = SECTION_LOGO;
|
||||
section->subsection = SUBSECTION_LOGO_TO_INTRO;
|
||||
options.section.name = SECTION_LOGO;
|
||||
options.section.subsection = SUBSECTION_LOGO_TO_INTRO;
|
||||
}
|
||||
|
||||
else if (input->checkInput(input_toggle_border, REPEAT_FALSE))
|
||||
{
|
||||
screen->switchBorder();
|
||||
screen->toggleBorder();
|
||||
}
|
||||
|
||||
else if (input->checkInput(input_window_fullscreen, REPEAT_FALSE))
|
||||
else if (input->checkInput(input_toggle_videomode, REPEAT_FALSE))
|
||||
{
|
||||
screen->switchVideoMode();
|
||||
screen->toggleVideoMode();
|
||||
}
|
||||
|
||||
else if (input->checkInput(input_window_dec_size, REPEAT_FALSE))
|
||||
@@ -222,7 +229,7 @@ void Ending2::checkInput()
|
||||
screen->incWindowSize();
|
||||
}
|
||||
|
||||
else if (input->checkInput(input_swap_palette, REPEAT_FALSE))
|
||||
else if (input->checkInput(input_toggle_palette, REPEAT_FALSE))
|
||||
{
|
||||
switchPalette();
|
||||
}
|
||||
@@ -233,7 +240,7 @@ void Ending2::run()
|
||||
{
|
||||
JA_PlayMusic(music);
|
||||
|
||||
while (section->name == SECTION_ENDING2)
|
||||
while (options.section.name == SECTION_ENDING2)
|
||||
{
|
||||
update();
|
||||
checkEvents();
|
||||
@@ -264,8 +271,8 @@ void Ending2::updateCounters()
|
||||
|
||||
if (postCounter > 600)
|
||||
{
|
||||
section->name = SECTION_LOGO;
|
||||
section->subsection = SUBSECTION_LOGO_TO_INTRO;
|
||||
options.section.name = SECTION_LOGO;
|
||||
options.section.subsection = SUBSECTION_LOGO_TO_INTRO;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -403,7 +410,7 @@ void Ending2::updateTexts()
|
||||
// Dibuja los sprites
|
||||
void Ending2::renderSprites()
|
||||
{
|
||||
const color_t color = stringToColor(options->palette, "red");
|
||||
const color_t color = stringToColor(options.palette, "red");
|
||||
for (auto sprite : sprites)
|
||||
{
|
||||
const bool a = sprite->getRect().y + sprite->getRect().h > 0;
|
||||
@@ -416,7 +423,7 @@ void Ending2::renderSprites()
|
||||
}
|
||||
|
||||
// Pinta el ultimo elemento de otro color
|
||||
const color_t c = stringToColor(options->palette, "white");
|
||||
const color_t c = stringToColor(options.palette, "white");
|
||||
sprites.back()->getTexture()->setColor(c.r, c.g, c.b);
|
||||
sprites.back()->render();
|
||||
}
|
||||
@@ -424,7 +431,7 @@ void Ending2::renderSprites()
|
||||
// Dibuja los sprites con el texto
|
||||
void Ending2::renderSpriteTexts()
|
||||
{
|
||||
const color_t color = stringToColor(options->palette, "white");
|
||||
const color_t color = stringToColor(options.palette, "white");
|
||||
for (auto sprite : spriteTexts)
|
||||
{
|
||||
const bool a = sprite->getRect().y + sprite->getRect().h > 0;
|
||||
@@ -631,5 +638,5 @@ void Ending2::updateMusicVolume()
|
||||
// Cambia la paleta
|
||||
void Ending2::switchPalette()
|
||||
{
|
||||
options->palette = (options->palette == p_zxspectrum) ? p_zxarne : p_zxspectrum;
|
||||
options.palette = (options.palette == p_zxspectrum) ? p_zxarne : p_zxspectrum;
|
||||
}
|
||||
@@ -1,38 +1,35 @@
|
||||
#pragma once
|
||||
#include <SDL2/SDL.h>
|
||||
#include "jail_engine/animatedsprite.h"
|
||||
#include "jail_engine/asset.h"
|
||||
#include "jail_engine/input.h"
|
||||
#include "jail_engine/jail_audio.h"
|
||||
#include "jail_engine/resource.h"
|
||||
#include "jail_engine/screen.h"
|
||||
#include "jail_engine/sprite.h"
|
||||
#include "jail_engine/text.h"
|
||||
#include "jail_engine/texture.h"
|
||||
#include "jail_engine/utils.h"
|
||||
#include "const.h"
|
||||
#include <vector>
|
||||
#include <string>
|
||||
|
||||
#ifndef ENDING2_H
|
||||
#define ENDING2_H
|
||||
#include <SDL2/SDL_events.h> // for SDL_Event
|
||||
#include <SDL2/SDL_render.h> // for SDL_Renderer
|
||||
#include <SDL2/SDL_stdinc.h> // for Uint32
|
||||
#include <string> // for string
|
||||
#include <vector> // for vector
|
||||
class AnimatedSprite; // lines 9-9
|
||||
class Asset; // lines 10-10
|
||||
class Input; // lines 11-11
|
||||
class MovingSprite; // lines 12-12
|
||||
class Resource; // lines 13-13
|
||||
class Screen; // lines 14-14
|
||||
class Text; // lines 15-15
|
||||
struct JA_Music_t; // lines 16-16
|
||||
struct color_t;
|
||||
struct options_t;
|
||||
struct section_t;
|
||||
|
||||
class Ending2
|
||||
{
|
||||
private:
|
||||
// Objetos y punteros
|
||||
Asset *asset; // Objeto con los ficheros de recursos
|
||||
Resource *resource; // Objeto con los recursos
|
||||
Screen *screen; // Objeto encargado de dibujar en pantalla
|
||||
SDL_Event *eventHandler; // Manejador de eventos
|
||||
SDL_Renderer *renderer; // El renderizador de la ventana
|
||||
Resource *resource; // Objeto con los recursos
|
||||
Asset *asset; // Objeto con los ficheros de recursos
|
||||
Input *input; // Objeto pata gestionar la entrada
|
||||
Text *text; // Objeto para escribir texto en pantalla
|
||||
options_t *options; // Puntero a las opciones del juego
|
||||
std::vector<AnimatedSprite *> sprites; // Vector con todos los sprites a dibujar
|
||||
std::vector<MovingSprite *> spriteTexts; // Vector con los sprites de texto de los sprites
|
||||
std::vector<MovingSprite *> texts; // Vector con los sprites de texto
|
||||
section_t *section; // Estado del bucle principal para saber si continua o se sale
|
||||
|
||||
// Variables
|
||||
bool counterEnabled; // Indica si está el contador habilitado
|
||||
@@ -120,7 +117,7 @@ private:
|
||||
|
||||
public:
|
||||
// Constructor
|
||||
Ending2(SDL_Renderer *renderer, Screen *screen, Resource *resource, Asset *asset, Input *input, options_t *options, section_t *section);
|
||||
Ending2();
|
||||
|
||||
// Destructor
|
||||
~Ending2();
|
||||
@@ -128,5 +125,3 @@ public:
|
||||
// Bucle principal
|
||||
void run();
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -1,4 +1,8 @@
|
||||
#include "enemy.h"
|
||||
#include <stdlib.h> // Para rand
|
||||
#include <algorithm> // Para min
|
||||
#include "animatedsprite.h" // Para AnimatedSprite
|
||||
#include "texture.h" // Para Texture
|
||||
|
||||
// Constructor
|
||||
Enemy::Enemy(enemy_t enemy)
|
||||
|
||||
@@ -1,13 +1,11 @@
|
||||
#pragma once
|
||||
|
||||
#include <SDL2/SDL.h>
|
||||
#include "jail_engine/animatedsprite.h"
|
||||
#include "jail_engine/asset.h"
|
||||
#include "jail_engine/utils.h"
|
||||
#include <string>
|
||||
|
||||
#ifndef ENEMY_H
|
||||
#define ENEMY_H
|
||||
#include <SDL2/SDL_rect.h> // Para SDL_Rect
|
||||
#include <SDL2/SDL_render.h> // Para SDL_Renderer
|
||||
#include <string> // Para basic_string, string
|
||||
#include "utils.h" // Para palette_e, color_t
|
||||
class AnimatedSprite;
|
||||
struct animatedSprite_t;
|
||||
|
||||
// Estructura para pasar los datos de un enemigo
|
||||
struct enemy_t
|
||||
@@ -78,5 +76,3 @@ public:
|
||||
// Asigna la paleta
|
||||
void setPalette(palette_e pal);
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
@@ -1,326 +0,0 @@
|
||||
#include "jail_engine/jail_audio.h"
|
||||
#include "jail_engine/jscore.h"
|
||||
#include "const.h"
|
||||
#include "enter_id.h"
|
||||
#include <iostream>
|
||||
|
||||
// Constructor
|
||||
EnterID::EnterID(SDL_Renderer *renderer, Screen *screen, Asset *asset, options_t *options, section_t *section)
|
||||
{
|
||||
// Copia la dirección de los objetos
|
||||
this->renderer = renderer;
|
||||
this->screen = screen;
|
||||
this->asset = asset;
|
||||
this->options = options;
|
||||
this->section = section;
|
||||
|
||||
// Reserva memoria para los punteros
|
||||
eventHandler = new SDL_Event();
|
||||
texture = new Texture(renderer, asset->get("smb2.png"));
|
||||
text = new Text(asset->get("smb2.txt"), texture, renderer);
|
||||
|
||||
// Crea la textura para el texto que se escribe en pantalla
|
||||
textTexture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, GAMECANVAS_WIDTH, GAMECANVAS_HEIGHT);
|
||||
if (textTexture == nullptr)
|
||||
{
|
||||
if (options->console)
|
||||
{
|
||||
std::cout << "Error: textTexture could not be created!\nSDL Error: " << SDL_GetError() << std::endl;
|
||||
}
|
||||
}
|
||||
SDL_SetTextureBlendMode(textTexture, SDL_BLENDMODE_BLEND);
|
||||
|
||||
// Inicializa variables
|
||||
oldJailerID = options->online.jailerID;
|
||||
loopRunning = true;
|
||||
counter = 0;
|
||||
ticks = 0;
|
||||
ticksSpeed = 15;
|
||||
jailerIDPos = 0;
|
||||
initName();
|
||||
|
||||
// Escribe el texto en la textura
|
||||
fillTexture();
|
||||
}
|
||||
|
||||
// Destructor
|
||||
EnterID::~EnterID()
|
||||
{
|
||||
delete eventHandler;
|
||||
delete text;
|
||||
delete texture;
|
||||
SDL_DestroyTexture(textTexture);
|
||||
}
|
||||
|
||||
// Bucle para el logo del juego
|
||||
void EnterID::run()
|
||||
{
|
||||
while (loopRunning)
|
||||
{
|
||||
update();
|
||||
checkEvents();
|
||||
render();
|
||||
}
|
||||
}
|
||||
|
||||
// Comprueba el manejador de eventos
|
||||
void EnterID::checkEvents()
|
||||
{
|
||||
// Comprueba los eventos que hay en la cola
|
||||
while (SDL_PollEvent(eventHandler) != 0)
|
||||
{
|
||||
// Evento de salida de la aplicación
|
||||
if (eventHandler->type == SDL_QUIT)
|
||||
{
|
||||
section->name = SECTION_QUIT;
|
||||
loopRunning = false;
|
||||
break;
|
||||
}
|
||||
|
||||
// Comprueba las teclas que se han pulsado
|
||||
if ((eventHandler->type == SDL_KEYDOWN && eventHandler->key.repeat == 0) || (eventHandler->type == SDL_JOYBUTTONDOWN))
|
||||
{
|
||||
if (eventHandler->key.keysym.scancode == SDL_SCANCODE_RETURN)
|
||||
{
|
||||
options->online.jailerID = toLower((std::string)name);
|
||||
endSection();
|
||||
break;
|
||||
}
|
||||
|
||||
if (eventHandler->key.keysym.scancode >= SDL_SCANCODE_A && eventHandler->key.keysym.scancode <= SDL_SCANCODE_Z)
|
||||
{ // Si pulsa una letra
|
||||
if (pos < maxLenght)
|
||||
{
|
||||
name[pos++] = eventHandler->key.keysym.scancode + 61;
|
||||
name[pos] = 0;
|
||||
}
|
||||
}
|
||||
|
||||
else if (eventHandler->key.keysym.scancode >= SDL_SCANCODE_1 && eventHandler->key.keysym.scancode <= SDL_SCANCODE_9)
|
||||
{ // Si pulsa un número
|
||||
if (pos < maxLenght)
|
||||
{ // En ascii el '0' va antes del '1', pero en scancode el '0' va despues de '9'
|
||||
name[pos++] = eventHandler->key.keysym.scancode + 19;
|
||||
name[pos] = 0;
|
||||
}
|
||||
}
|
||||
|
||||
else if (eventHandler->key.keysym.scancode == SDL_SCANCODE_0)
|
||||
{
|
||||
if (pos < maxLenght)
|
||||
{
|
||||
name[pos++] = 48;
|
||||
name[pos] = 0;
|
||||
}
|
||||
}
|
||||
|
||||
else if (eventHandler->key.keysym.scancode == SDL_SCANCODE_BACKSPACE)
|
||||
{
|
||||
if (pos > 0)
|
||||
{
|
||||
name[--pos] = 0;
|
||||
}
|
||||
}
|
||||
|
||||
else if (eventHandler->key.keysym.scancode == SDL_SCANCODE_ESCAPE)
|
||||
{
|
||||
section->name = SECTION_QUIT;
|
||||
loopRunning = false;
|
||||
break;
|
||||
}
|
||||
|
||||
else if (eventHandler->key.keysym.scancode == SDL_SCANCODE_F1)
|
||||
{
|
||||
screen->setWindowSize(1);
|
||||
break;
|
||||
}
|
||||
|
||||
else if (eventHandler->key.keysym.scancode == SDL_SCANCODE_F2)
|
||||
{
|
||||
screen->setWindowSize(2);
|
||||
break;
|
||||
}
|
||||
|
||||
else if (eventHandler->key.keysym.scancode == SDL_SCANCODE_F3)
|
||||
{
|
||||
screen->setWindowSize(3);
|
||||
break;
|
||||
}
|
||||
|
||||
else if (eventHandler->key.keysym.scancode == SDL_SCANCODE_F4)
|
||||
{
|
||||
screen->setWindowSize(4);
|
||||
break;
|
||||
}
|
||||
|
||||
else if (eventHandler->key.keysym.scancode == SDL_SCANCODE_F5)
|
||||
{
|
||||
switchPalette();
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Actualiza las variables
|
||||
void EnterID::update()
|
||||
{
|
||||
// Comprueba que la diferencia de ticks sea mayor a la velocidad del juego
|
||||
if (SDL_GetTicks() - ticks > ticksSpeed)
|
||||
{
|
||||
// Actualiza el contador de ticks
|
||||
ticks = SDL_GetTicks();
|
||||
|
||||
// Actualiza el contador
|
||||
counter++;
|
||||
|
||||
// Actualiza el cursor
|
||||
cursor = (counter % 20 >= 10) ? " " : "_";
|
||||
|
||||
// Actualiza las notificaciones
|
||||
screen->updateNotifier();
|
||||
}
|
||||
}
|
||||
|
||||
// Dibuja en pantalla
|
||||
void EnterID::render()
|
||||
{
|
||||
// Prepara para empezar a dibujar en la textura de juego
|
||||
screen->start();
|
||||
|
||||
// Dibuja la textura con el texto en pantalla
|
||||
SDL_RenderCopy(renderer, textTexture, nullptr, nullptr);
|
||||
|
||||
// Escribe el jailerID
|
||||
const std::string jailerID = (std::string)name + cursor;
|
||||
const color_t color = stringToColor(options->palette, "white");
|
||||
text->writeDX(TXT_CENTER | TXT_COLOR, GAMECANVAS_CENTER_X, jailerIDPos, jailerID, 1, color);
|
||||
|
||||
// Vuelca el contenido del renderizador en pantalla
|
||||
screen->blit();
|
||||
}
|
||||
|
||||
// Inicializa los textos
|
||||
void EnterID::iniTexts()
|
||||
{
|
||||
texts.clear();
|
||||
texts.push_back({"", stringToColor(options->palette, "white")});
|
||||
texts.push_back({"", stringToColor(options->palette, "white")});
|
||||
texts.push_back({"", stringToColor(options->palette, "white")});
|
||||
texts.push_back({"", stringToColor(options->palette, "white")});
|
||||
texts.push_back({"", stringToColor(options->palette, "white")});
|
||||
texts.push_back({"", stringToColor(options->palette, "white")});
|
||||
texts.push_back({"ONLINE CONFIGURATION:", stringToColor(options->palette, "green")});
|
||||
texts.push_back({"", stringToColor(options->palette, "white")});
|
||||
texts.push_back({"PLEASE ENTER AN ID OR", stringToColor(options->palette, "white")});
|
||||
texts.push_back({"LEAVE BLANK FOR OFFLINE MODE", stringToColor(options->palette, "white")});
|
||||
texts.push_back({"", stringToColor(options->palette, "white")});
|
||||
texts.push_back({"", stringToColor(options->palette, "white")});
|
||||
texts.push_back({"", stringToColor(options->palette, "white")});
|
||||
texts.push_back({"JAILER_ID:", stringToColor(options->palette, "green")});
|
||||
|
||||
jailerIDPos = ((int)texts.size() + 1) * text->getCharacterSize();
|
||||
}
|
||||
|
||||
// Escribe el texto en la textura
|
||||
void EnterID::fillTexture()
|
||||
{
|
||||
// Inicializa los textos
|
||||
iniTexts();
|
||||
|
||||
// Rellena la textura de texto
|
||||
SDL_SetRenderTarget(renderer, textTexture);
|
||||
color_t c = stringToColor(options->palette, "black");
|
||||
SDL_SetRenderDrawColor(renderer, c.r, c.g, c.b, 0xFF);
|
||||
SDL_RenderClear(renderer);
|
||||
|
||||
// Escribe el texto en la textura
|
||||
const int size = text->getCharacterSize();
|
||||
int i = 0;
|
||||
|
||||
for (auto t : texts)
|
||||
{
|
||||
text->writeDX(TXT_CENTER | TXT_COLOR, PLAY_AREA_CENTER_X, i * size, t.label, 1, t.color);
|
||||
i++;
|
||||
}
|
||||
|
||||
SDL_SetRenderTarget(renderer, nullptr);
|
||||
}
|
||||
|
||||
// Inicializa los servicios online
|
||||
void EnterID::initOnline()
|
||||
{
|
||||
// Si ya ha iniciado la sesión y no ha cambiado el jailerID, que no continue
|
||||
if (options->online.sessionEnabled)
|
||||
{
|
||||
if (oldJailerID == options->online.jailerID)
|
||||
{
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
if (options->online.jailerID == "")
|
||||
{ // Jailer ID no definido
|
||||
options->online.enabled = false;
|
||||
options->online.sessionEnabled = false;
|
||||
}
|
||||
else
|
||||
{ // Jailer ID iniciado
|
||||
options->online.enabled = options->online.sessionEnabled = true;
|
||||
// Establece el servidor y el puerto
|
||||
jscore::init(options->online.server, options->online.port);
|
||||
#ifdef DEBUG
|
||||
const std::string caption = "IS LOGGED IN (DEBUG)";
|
||||
#else
|
||||
const std::string caption = "IS LOGGED IN";
|
||||
#endif
|
||||
screen->showNotification(options->online.jailerID, caption, 12);
|
||||
if (options->console)
|
||||
{
|
||||
std::cout << caption << std::endl;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Termina la sección
|
||||
void EnterID::endSection()
|
||||
{
|
||||
loopRunning = false;
|
||||
initOnline();
|
||||
}
|
||||
|
||||
// Inicializa el vector utilizado para almacenar el texto que se escribe en pantalla
|
||||
void EnterID::initName()
|
||||
{
|
||||
// Calcula el tamaño del vector
|
||||
name[0] = 0;
|
||||
maxLenght = sizeof(name) / sizeof(name[pos]);
|
||||
|
||||
// Inicializa el vector con ceros
|
||||
for (int i = 0; i < maxLenght; ++i)
|
||||
{
|
||||
name[i] = 0;
|
||||
}
|
||||
|
||||
// Si no hay definido ningun JailerID, coloca el cursor en primera posición
|
||||
if (options->online.jailerID == "")
|
||||
{
|
||||
pos = 0;
|
||||
}
|
||||
else
|
||||
{ // En caso contrario, copia el texto al vector y coloca el cursor en posición
|
||||
const int len = std::min((int)options->online.jailerID.size(), maxLenght);
|
||||
for (int i = 0; i < len; ++i)
|
||||
{
|
||||
name[i] = (char)options->online.jailerID[i];
|
||||
}
|
||||
pos = len;
|
||||
}
|
||||
}
|
||||
|
||||
// Cambia la paleta
|
||||
void EnterID::switchPalette()
|
||||
{
|
||||
options->palette = options->palette == p_zxspectrum ? p_zxarne : p_zxspectrum;
|
||||
fillTexture();
|
||||
}
|
||||
@@ -1,86 +0,0 @@
|
||||
#pragma once
|
||||
#include <SDL2/SDL.h>
|
||||
#include "jail_engine/asset.h"
|
||||
#include "jail_engine/screen.h"
|
||||
#include "jail_engine/utils.h"
|
||||
#include "jail_engine/text.h"
|
||||
#include "jail_engine/texture.h"
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#ifndef ENTER_ID_H
|
||||
#define ENTER_ID_H
|
||||
|
||||
class EnterID
|
||||
{
|
||||
private:
|
||||
struct captions_t
|
||||
{
|
||||
std::string label; // Texto a escribir
|
||||
color_t color; // Color del texto
|
||||
};
|
||||
|
||||
// Punteros y objetos
|
||||
Asset *asset; // Objeto con los ficheros de recursos
|
||||
options_t *options; // Puntero a las opciones del juego
|
||||
Screen *screen; // Objeto encargado de dibujar en pantalla
|
||||
SDL_Event *eventHandler; // Manejador de eventos
|
||||
SDL_Renderer *renderer; // El renderizador de la ventana
|
||||
SDL_Texture *textTexture; // Textura para dibujar el texto
|
||||
Text *text; // Objeto para escribir texto en pantalla
|
||||
Texture *texture; // Textura para la fuente para el texto
|
||||
section_t *section; // Estado del bucle principal para saber si continua o se sale
|
||||
|
||||
// Variables
|
||||
bool loopRunning; // Indica si ha de terminar el bucle principal
|
||||
int counter; // Contador
|
||||
Uint32 ticks; // Contador de ticks para ajustar la velocidad del programa
|
||||
Uint32 ticksSpeed; // Velocidad a la que se repiten los bucles del programa
|
||||
std::vector<captions_t> texts; // Vector con los textos
|
||||
std::string cursor; // Contiene el caracter que se muestra como cursor
|
||||
|
||||
char name[15]; // Aqui se guardan los caracteres de las teclas que se van pulsando
|
||||
int pos; // Posición actual en el vector name
|
||||
int maxLenght; // Tamaño máximo del jailerID
|
||||
std::string oldJailerID; // Almacena el valor de jailerID al inicio para ver si se ha modificado
|
||||
int jailerIDPos; // Posición en el eje Y donde ser va a escribir el texto
|
||||
|
||||
// Actualiza las variables
|
||||
void update();
|
||||
|
||||
// Dibuja en pantalla
|
||||
void render();
|
||||
|
||||
// Comprueba el manejador de eventos
|
||||
void checkEvents();
|
||||
|
||||
// Inicializa los textos
|
||||
void iniTexts();
|
||||
|
||||
// Escribe el texto en la textura
|
||||
void fillTexture();
|
||||
|
||||
// Inicializa los servicios online
|
||||
void initOnline();
|
||||
|
||||
// Termina la sección
|
||||
void endSection();
|
||||
|
||||
// Inicializa el vector utilizado para almacenar el texto que se escribe en pantalla
|
||||
void initName();
|
||||
|
||||
// Cambia la paleta
|
||||
void switchPalette();
|
||||
|
||||
public:
|
||||
// Constructor
|
||||
EnterID(SDL_Renderer *renderer, Screen *screen, Asset *asset, options_t *options, section_t *section);
|
||||
|
||||
// Destructor
|
||||
~EnterID();
|
||||
|
||||
// Bucle principal
|
||||
void run();
|
||||
};
|
||||
|
||||
#endif
|
||||
799
source/game.cpp
Normal file
799
source/game.cpp
Normal file
@@ -0,0 +1,799 @@
|
||||
#include "game.h"
|
||||
#include <SDL2/SDL_blendmode.h> // for SDL_BLENDMODE_BLEND
|
||||
#include <SDL2/SDL_error.h> // for SDL_GetError
|
||||
#include <SDL2/SDL_pixels.h> // for SDL_PIXELFORMAT_RGBA8888
|
||||
#include <SDL2/SDL_scancode.h> // for SDL_SCANCODE_A, SDL_SCANCODE_D, SDL_...
|
||||
#include <SDL2/SDL_timer.h> // for SDL_GetTicks
|
||||
#include <iostream> // for basic_ostream, operator<<, cout, endl
|
||||
#include <vector> // for vector
|
||||
#include "asset.h" // for Asset
|
||||
#include "cheevos.h" // for Cheevos
|
||||
#include "const.h" // for PLAY_AREA_HEIGHT, GAMECANVAS_WIDTH
|
||||
#include "debug.h" // for Debug
|
||||
#include "input.h" // for Input, REPEAT_FALSE, inputs_e
|
||||
#include "item_tracker.h" // for ItemTracker
|
||||
#include "jail_audio.h" // for JA_PauseMusic, JA_PlaySound, JA_Resu...
|
||||
#include "resource.h" // for Resource, res_room_t
|
||||
#include "room.h" // for Room, room_t
|
||||
#include "room_tracker.h" // for RoomTracker
|
||||
#include "screen.h" // for Screen
|
||||
#include "stats.h" // for Stats
|
||||
#include "text.h" // for Text, TXT_CENTER, TXT_COLOR
|
||||
#include "utils.h" // for options_t, cheat_t, stringToColor
|
||||
#include "options.h"
|
||||
#include "notifier.h"
|
||||
|
||||
// Constructor
|
||||
Game::Game()
|
||||
: screen_(Screen::get()),
|
||||
renderer_(Screen::get()->getRenderer()),
|
||||
asset_(Asset::get()),
|
||||
input_(Input::get()),
|
||||
resource_(Resource::get()),
|
||||
debug_(Debug::get())
|
||||
{
|
||||
// Inicia algunas variables
|
||||
board_.iniClock = SDL_GetTicks();
|
||||
#ifdef DEBUG
|
||||
current_room_ = "03.room";
|
||||
const int x = 25;
|
||||
const int y = 13;
|
||||
spawn_point_ = {x * 8, y * 8, 0, 0, 0, s_standing, SDL_FLIP_HORIZONTAL};
|
||||
debug_->setEnabled(false);
|
||||
#else
|
||||
current_room_ = "03.room";
|
||||
const int x = 25;
|
||||
const int y = 13;
|
||||
spawn_point_ = {x * 8, y * 8, 0, 0, 0, s_standing, SDL_FLIP_HORIZONTAL};
|
||||
#endif
|
||||
|
||||
// Crea los objetos
|
||||
cheevos_ = Cheevos::get();
|
||||
scoreboard_ = new Scoreboard(&board_);
|
||||
item_tracker_ = new ItemTracker();
|
||||
room_tracker_ = new RoomTracker();
|
||||
room_ = new Room(resource_->getRoom(current_room_), item_tracker_, &board_.items, false);
|
||||
const std::string playerPNG = options.cheat.altSkin ? "player2.png" : "player.png";
|
||||
const std::string playerANI = options.cheat.altSkin ? "player2.ani" : "player.ani";
|
||||
const player_t player = {spawn_point_, playerPNG, playerANI, room_};
|
||||
player_ = new Player(player);
|
||||
text_ = new Text(resource_->getOffset("smb2.txt"), resource_->getTexture("smb2.png"), renderer_);
|
||||
music_ = JA_LoadMusic(asset_->get("game.ogg").c_str());
|
||||
death_sound_ = JA_LoadSound(asset_->get("death.wav").c_str());
|
||||
stats_ = new Stats(asset_->get("stats.csv"), asset_->get("stats_buffer.csv"));
|
||||
|
||||
// Crea la textura para poner el nombre de la habitación
|
||||
room_name_texture_ = SDL_CreateTexture(renderer_, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, GAMECANVAS_WIDTH, text_->getCharacterSize() * 2);
|
||||
if (room_name_texture_ == nullptr)
|
||||
{
|
||||
if (options.console)
|
||||
{
|
||||
std::cout << "Error: roomNameTexture could not be created!\nSDL Error: " << SDL_GetError() << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
// Establece el blend mode de la textura
|
||||
SDL_SetTextureBlendMode(room_name_texture_, SDL_BLENDMODE_BLEND);
|
||||
|
||||
// Establece el destino de la textura
|
||||
room_name_rect_ = {0, PLAY_AREA_HEIGHT, GAMECANVAS_WIDTH, text_->getCharacterSize() * 2};
|
||||
|
||||
// Pone el nombre de la habitación en la textura
|
||||
fillRoomNameTexture();
|
||||
|
||||
// Inicializa el resto de variables
|
||||
ticks_ = 0;
|
||||
ticks_speed_ = 15;
|
||||
board_.lives = 9;
|
||||
#ifdef DEBUG
|
||||
board_.lives = 9;
|
||||
#endif
|
||||
board_.items = 0;
|
||||
board_.rooms = 1;
|
||||
board_.music = true;
|
||||
board_.jailEnabled = options.cheat.jailEnabled;
|
||||
setScoreBoardColor();
|
||||
room_tracker_->addRoom(current_room_);
|
||||
paused_ = false;
|
||||
black_screen_ = false;
|
||||
black_screen_counter_ = 0;
|
||||
total_items_ = getTotalItems();
|
||||
initStats();
|
||||
stats_->addVisit(room_->getName());
|
||||
const bool cheats = options.cheat.infiniteLives || options.cheat.invincible || options.cheat.jailEnabled;
|
||||
cheevos_->enable(!cheats); // Deshabilita los logros si hay trucos activados
|
||||
|
||||
options.section.name = SECTION_GAME;
|
||||
options.section.subsection = 0;
|
||||
}
|
||||
|
||||
Game::~Game()
|
||||
{
|
||||
// Libera la memoria de los objetos
|
||||
delete scoreboard_;
|
||||
delete item_tracker_;
|
||||
delete room_tracker_;
|
||||
delete room_;
|
||||
delete player_;
|
||||
delete text_;
|
||||
delete stats_;
|
||||
|
||||
SDL_DestroyTexture(room_name_texture_);
|
||||
|
||||
JA_DeleteMusic(music_);
|
||||
JA_DeleteSound(death_sound_);
|
||||
}
|
||||
|
||||
// Comprueba los eventos de la cola
|
||||
void Game::checkEvents()
|
||||
{
|
||||
SDL_Event event;
|
||||
while (SDL_PollEvent(&event))
|
||||
{
|
||||
// Evento de salida de la aplicación
|
||||
if (event.type == SDL_QUIT)
|
||||
{
|
||||
options.section.name = SECTION_QUIT;
|
||||
screen_->setBorderColor(stringToColor(options.palette, "black"));
|
||||
break;
|
||||
}
|
||||
|
||||
if (event.type == SDL_RENDER_DEVICE_RESET || event.type == SDL_RENDER_TARGETS_RESET)
|
||||
{
|
||||
reLoadTextures();
|
||||
}
|
||||
|
||||
if (event.type == SDL_KEYDOWN && event.key.repeat == 0)
|
||||
{
|
||||
switch (event.key.keysym.scancode)
|
||||
{
|
||||
#ifdef DEBUG
|
||||
case SDL_SCANCODE_G:
|
||||
debug_->switchEnabled();
|
||||
options.cheat.invincible = debug_->getEnabled();
|
||||
board_.music = !debug_->getEnabled();
|
||||
board_.music ? JA_ResumeMusic() : JA_PauseMusic();
|
||||
break;
|
||||
|
||||
case SDL_SCANCODE_R:
|
||||
resource_->reLoad();
|
||||
break;
|
||||
|
||||
case SDL_SCANCODE_W:
|
||||
goToRoom(BORDER_TOP);
|
||||
break;
|
||||
|
||||
case SDL_SCANCODE_A:
|
||||
goToRoom(BORDER_LEFT);
|
||||
break;
|
||||
|
||||
case SDL_SCANCODE_S:
|
||||
goToRoom(BORDER_BOTTOM);
|
||||
break;
|
||||
|
||||
case SDL_SCANCODE_D:
|
||||
goToRoom(BORDER_RIGHT);
|
||||
break;
|
||||
|
||||
case SDL_SCANCODE_F6:
|
||||
Notifier::get()->show("ACHIEVEMENT UNLOCKED!", "I LIKE MY MULTICOLOURED FRIENDS", 2);
|
||||
break;
|
||||
|
||||
case SDL_SCANCODE_F7:
|
||||
Notifier::get()->show("ACHIEVEMENT UNLOCKED!", "I LIKE MY MULTICOLOURED FRIENDS", 3);
|
||||
break;
|
||||
|
||||
case SDL_SCANCODE_F8:
|
||||
Notifier::get()->show("JAILDESIGNER IS LOGGED IN", "", 4);
|
||||
break;
|
||||
|
||||
case SDL_SCANCODE_F9:
|
||||
Notifier::get()->show("JAILDESIGNER IS LOGGED IN", "", 5);
|
||||
break;
|
||||
#endif
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Comprueba el teclado
|
||||
void Game::checkInput()
|
||||
{
|
||||
if (input_->checkInput(input_exit, REPEAT_FALSE))
|
||||
{
|
||||
options.section.name = SECTION_TITLE;
|
||||
}
|
||||
|
||||
else if (input_->checkInput(input_toggle_music, REPEAT_FALSE))
|
||||
{
|
||||
board_.music = !board_.music;
|
||||
board_.music ? JA_ResumeMusic() : JA_PauseMusic();
|
||||
}
|
||||
|
||||
else if (input_->checkInput(input_pause, REPEAT_FALSE))
|
||||
{
|
||||
switchPause();
|
||||
}
|
||||
|
||||
else if (input_->checkInput(input_toggle_border, REPEAT_FALSE))
|
||||
{
|
||||
screen_->toggleBorder();
|
||||
reLoadTextures();
|
||||
}
|
||||
|
||||
else if (input_->checkInput(input_toggle_videomode, REPEAT_FALSE))
|
||||
{
|
||||
screen_->toggleVideoMode();
|
||||
reLoadTextures();
|
||||
}
|
||||
|
||||
else if (input_->checkInput(input_toggle_shaders, REPEAT_FALSE))
|
||||
{
|
||||
screen_->toggleShaders();
|
||||
}
|
||||
|
||||
else if (input_->checkInput(input_window_dec_size, REPEAT_FALSE))
|
||||
{
|
||||
screen_->decWindowSize();
|
||||
reLoadTextures();
|
||||
}
|
||||
|
||||
else if (input_->checkInput(input_window_inc_size, REPEAT_FALSE))
|
||||
{
|
||||
screen_->incWindowSize();
|
||||
reLoadTextures();
|
||||
}
|
||||
|
||||
else if (input_->checkInput(input_toggle_palette, REPEAT_FALSE))
|
||||
{
|
||||
switchPalette();
|
||||
}
|
||||
}
|
||||
|
||||
// Bucle para el juego
|
||||
void Game::run()
|
||||
{
|
||||
JA_PlayMusic(music_);
|
||||
if (!board_.music)
|
||||
{
|
||||
JA_PauseMusic();
|
||||
}
|
||||
|
||||
while (options.section.name == SECTION_GAME)
|
||||
{
|
||||
update();
|
||||
checkEvents();
|
||||
render();
|
||||
}
|
||||
|
||||
JA_StopMusic();
|
||||
}
|
||||
|
||||
// Actualiza el juego, las variables, comprueba la entrada, etc.
|
||||
void Game::update()
|
||||
{
|
||||
// Comprueba que la diferencia de ticks sea mayor a la velocidad del juego
|
||||
if (SDL_GetTicks() - ticks_ > ticks_speed_)
|
||||
{
|
||||
// Actualiza el contador de ticks
|
||||
ticks_ = SDL_GetTicks();
|
||||
|
||||
// Comprueba el teclado
|
||||
checkInput();
|
||||
|
||||
#ifdef DEBUG
|
||||
debug_->clear();
|
||||
#endif
|
||||
|
||||
// Actualiza los objetos
|
||||
room_->update();
|
||||
player_->update();
|
||||
checkPlayerOnBorder();
|
||||
checkPlayerAndItems();
|
||||
checkPlayerAndEnemies();
|
||||
checkIfPlayerIsAlive();
|
||||
checkGameOver();
|
||||
checkEndGame();
|
||||
checkRestoringJail();
|
||||
checkSomeCheevos();
|
||||
scoreboard_->update();
|
||||
input_->update();
|
||||
|
||||
updateBlackScreen();
|
||||
|
||||
screen_->update();
|
||||
|
||||
#ifdef DEBUG
|
||||
updateDebugInfo();
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
// Pinta los objetos en pantalla
|
||||
void Game::render()
|
||||
{
|
||||
// Prepara para dibujar el frame
|
||||
screen_->start();
|
||||
|
||||
// Dibuja los elementos del juego en orden
|
||||
room_->renderMap();
|
||||
room_->renderEnemies();
|
||||
room_->renderItems();
|
||||
player_->render();
|
||||
renderRoomName();
|
||||
scoreboard_->render();
|
||||
renderBlackScreen();
|
||||
|
||||
#ifdef DEBUG
|
||||
// Debug info
|
||||
renderDebugInfo();
|
||||
#endif
|
||||
|
||||
// Actualiza la pantalla
|
||||
screen_->render();
|
||||
}
|
||||
|
||||
#ifdef DEBUG
|
||||
// Pasa la información de debug
|
||||
void Game::updateDebugInfo()
|
||||
{
|
||||
debug_->add("X = " + std::to_string(static_cast<int>(player_->x)) + ", Y = " + std::to_string(static_cast<int>(player_->y)));
|
||||
debug_->add("VX = " + std::to_string(player_->vx).substr(0, 4) + ", VY = " + std::to_string(player_->vy).substr(0, 4));
|
||||
debug_->add("STATE = " + std::to_string(player_->state));
|
||||
}
|
||||
|
||||
// Pone la información de debug en pantalla
|
||||
void Game::renderDebugInfo()
|
||||
{
|
||||
if (!debug_->getEnabled())
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
// Borra el marcador
|
||||
SDL_Rect rect = {0, 18 * BLOCK, PLAY_AREA_WIDTH, GAMECANVAS_HEIGHT - PLAY_AREA_HEIGHT};
|
||||
SDL_SetRenderDrawColor(renderer_, 0, 0, 0, 255);
|
||||
SDL_RenderFillRect(renderer_, &rect);
|
||||
|
||||
// Pinta la rejilla
|
||||
SDL_SetRenderDrawColor(renderer_, 255, 255, 255, 32);
|
||||
for (int i = 0; i < PLAY_AREA_BOTTOM; i += 8)
|
||||
{ // Lineas horizontales
|
||||
SDL_RenderDrawLine(renderer_, 0, i, PLAY_AREA_RIGHT, i);
|
||||
}
|
||||
for (int i = 0; i < PLAY_AREA_RIGHT; i += 8)
|
||||
{ // Lineas verticales
|
||||
SDL_RenderDrawLine(renderer_, i, 0, i, PLAY_AREA_BOTTOM - 1);
|
||||
}
|
||||
|
||||
// Pinta el texto
|
||||
debug_->setPos({1, 18 * 8});
|
||||
debug_->render();
|
||||
}
|
||||
#endif
|
||||
|
||||
// Escribe el nombre de la pantalla
|
||||
void Game::renderRoomName()
|
||||
{
|
||||
// Dibuja la textura con el nombre de la habitación
|
||||
SDL_RenderCopy(renderer_, room_name_texture_, nullptr, &room_name_rect_);
|
||||
}
|
||||
|
||||
// Cambia de habitación
|
||||
bool Game::changeRoom(std::string file)
|
||||
{
|
||||
// En las habitaciones los limites tienen la cadena del fichero o un 0 en caso de no limitar con nada
|
||||
if (file == "0")
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
// Verifica que exista el fichero que se va a cargar
|
||||
if (asset_->get(file) != "")
|
||||
{
|
||||
// Elimina la habitación actual
|
||||
delete room_;
|
||||
room_ = nullptr;
|
||||
|
||||
// Crea un objeto habitación nuevo a partir del fichero
|
||||
room_ = new Room(resource_->getRoom(file), item_tracker_, &board_.items, board_.jailEnabled);
|
||||
|
||||
// Pone el nombre de la habitación en la textura
|
||||
fillRoomNameTexture();
|
||||
|
||||
// Pone el color del marcador en función del color del borde de la habitación
|
||||
setScoreBoardColor();
|
||||
|
||||
if (room_tracker_->addRoom(file))
|
||||
{
|
||||
// Incrementa el contador de habitaciones visitadas
|
||||
board_.rooms++;
|
||||
options.stats.rooms = board_.rooms;
|
||||
|
||||
// Actualiza las estadisticas
|
||||
stats_->addVisit(room_->getName());
|
||||
}
|
||||
|
||||
// Pasa la nueva habitación al jugador
|
||||
player_->setRoom(room_);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
// Comprueba si el jugador esta en el borde de la pantalla
|
||||
void Game::checkPlayerOnBorder()
|
||||
{
|
||||
if (player_->getOnBorder())
|
||||
{
|
||||
const std::string roomName = room_->getRoom(player_->getBorder());
|
||||
if (changeRoom(roomName))
|
||||
{
|
||||
player_->switchBorders();
|
||||
current_room_ = roomName;
|
||||
spawn_point_ = player_->getSpawnParams();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Comprueba las colisiones del jugador con los enemigos
|
||||
bool Game::checkPlayerAndEnemies()
|
||||
{
|
||||
const bool death = room_->enemyCollision(player_->getCollider());
|
||||
if (death)
|
||||
{
|
||||
killPlayer();
|
||||
}
|
||||
return death;
|
||||
}
|
||||
|
||||
// Comprueba las colisiones del jugador con los objetos
|
||||
void Game::checkPlayerAndItems()
|
||||
{
|
||||
room_->itemCollision(player_->getCollider());
|
||||
}
|
||||
|
||||
// Comprueba si el jugador esta vivo
|
||||
void Game::checkIfPlayerIsAlive()
|
||||
{
|
||||
if (!player_->isAlive())
|
||||
{
|
||||
killPlayer();
|
||||
}
|
||||
}
|
||||
|
||||
// Comprueba si ha terminado la partida
|
||||
void Game::checkGameOver()
|
||||
{
|
||||
if (board_.lives < 0 && black_screen_counter_ > 17)
|
||||
{
|
||||
options.section.name = SECTION_GAME_OVER;
|
||||
}
|
||||
}
|
||||
|
||||
// Mata al jugador
|
||||
void Game::killPlayer()
|
||||
{
|
||||
if (options.cheat.invincible)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
// Resta una vida al jugador
|
||||
if (!options.cheat.infiniteLives)
|
||||
{
|
||||
board_.lives--;
|
||||
}
|
||||
|
||||
// Actualiza las estadisticas
|
||||
stats_->addDeath(room_->getName());
|
||||
|
||||
// Invalida el logro de pasarse el juego sin morir
|
||||
cheevos_->invalidate(11);
|
||||
|
||||
// Destruye la habitacion y el jugador
|
||||
delete room_;
|
||||
delete this->player_;
|
||||
|
||||
// Sonido
|
||||
JA_PlaySound(death_sound_);
|
||||
|
||||
// Pone la pantalla en negro un tiempo
|
||||
setBlackScreen();
|
||||
|
||||
// Crea la nueva habitación y el nuevo jugador
|
||||
room_ = new Room(resource_->getRoom(current_room_),item_tracker_, &board_.items, board_.jailEnabled);
|
||||
const std::string playerPNG = options.cheat.altSkin ? "player2.png" : "player.png";
|
||||
const std::string playerANI = options.cheat.altSkin ? "player2.ani" : "player.ani";
|
||||
const player_t player = {spawn_point_, playerPNG, playerANI, room_};
|
||||
this->player_ = new Player(player);
|
||||
|
||||
// Pone los objetos en pausa mientras esta la habitación en negro
|
||||
room_->pause();
|
||||
this->player_->pause();
|
||||
}
|
||||
|
||||
// Recarga todas las texturas
|
||||
void Game::reLoadTextures()
|
||||
{
|
||||
if (options.console)
|
||||
{
|
||||
std::cout << "** RELOAD REQUESTED" << std::endl;
|
||||
}
|
||||
player_->reLoadTexture();
|
||||
room_->reLoadTexture();
|
||||
scoreboard_->reLoadTexture();
|
||||
text_->reLoadTexture();
|
||||
}
|
||||
|
||||
// Cambia la paleta
|
||||
void Game::switchPalette()
|
||||
{
|
||||
if (options.console)
|
||||
{
|
||||
std::cout << "** PALETTE SWITCH REQUESTED" << std::endl;
|
||||
}
|
||||
|
||||
// Modifica la variable
|
||||
options.palette = (options.palette == p_zxspectrum) ? p_zxarne : p_zxspectrum;
|
||||
|
||||
// Recarga las paletas
|
||||
room_->reLoadPalette();
|
||||
player_->reLoadPalette();
|
||||
scoreboard_->reLoadPalette();
|
||||
|
||||
// Pone el color del marcador en función del color del borde de la habitación
|
||||
setScoreBoardColor();
|
||||
}
|
||||
|
||||
// Establece la pantalla en negro
|
||||
void Game::setBlackScreen()
|
||||
{
|
||||
black_screen_ = true;
|
||||
}
|
||||
|
||||
// Actualiza las variables relativas a la pantalla en negro
|
||||
void Game::updateBlackScreen()
|
||||
{
|
||||
if (black_screen_)
|
||||
{
|
||||
black_screen_counter_++;
|
||||
if (black_screen_counter_ > 20)
|
||||
{
|
||||
black_screen_ = false;
|
||||
black_screen_counter_ = 0;
|
||||
|
||||
player_->resume();
|
||||
room_->resume();
|
||||
screen_->setBorderColor(room_->getBorderColor());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Dibuja la pantalla negra
|
||||
void Game::renderBlackScreen()
|
||||
{
|
||||
if (black_screen_)
|
||||
{
|
||||
screen_->clean();
|
||||
screen_->setBorderColor(stringToColor(options.palette, "black"));
|
||||
}
|
||||
}
|
||||
|
||||
// Pone el color del marcador en función del color del borde de la habitación
|
||||
void Game::setScoreBoardColor()
|
||||
{
|
||||
// Obtiene el color del borde
|
||||
const color_t colorBorder = room_->getBorderColor();
|
||||
|
||||
const bool isBlack = colorAreEqual(colorBorder, stringToColor(options.palette, "black"));
|
||||
const bool isBrightBlack = colorAreEqual(colorBorder, stringToColor(options.palette, "bright_black"));
|
||||
|
||||
// Si el color del borde es negro o negro brillante cambia el texto del marcador a blanco
|
||||
board_.color = isBlack || isBrightBlack ? stringToColor(options.palette, "white") : colorBorder;
|
||||
}
|
||||
|
||||
// Comprueba si ha finalizado el juego
|
||||
bool Game::checkEndGame()
|
||||
{
|
||||
const bool isOnTheRoom = room_->getName() == "THE JAIL"; // Estar en la habitación que toca
|
||||
const bool haveTheItems = board_.items >= int(total_items_ * 0.9f) || options.cheat.jailEnabled; // Con mas del 90% de los items recogidos
|
||||
const bool isOnTheDoor = player_->getRect().x <= 128; // Y en la ubicación que toca (En la puerta)
|
||||
|
||||
if (haveTheItems)
|
||||
{
|
||||
board_.jailEnabled = true;
|
||||
}
|
||||
|
||||
if (haveTheItems && isOnTheRoom && isOnTheDoor)
|
||||
{
|
||||
// Comprueba los logros de completar el juego
|
||||
checkEndGameCheevos();
|
||||
|
||||
options.section.name = SECTION_ENDING;
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
// Obtiene la cantidad total de items que hay en el mapeado del juego
|
||||
int Game::getTotalItems()
|
||||
{
|
||||
int items = 0;
|
||||
std::vector<res_room_t> *rooms = new std::vector<res_room_t>;
|
||||
rooms = resource_->getAllRooms();
|
||||
|
||||
for (auto room : *rooms)
|
||||
{
|
||||
items += room.room->items.size();
|
||||
}
|
||||
|
||||
return items;
|
||||
}
|
||||
|
||||
// Va a la habitación designada
|
||||
void Game::goToRoom(int border)
|
||||
{
|
||||
const std::string roomName = room_->getRoom(border);
|
||||
if (changeRoom(roomName))
|
||||
{
|
||||
current_room_ = roomName;
|
||||
}
|
||||
}
|
||||
|
||||
// Pone el juego en pausa
|
||||
void Game::switchPause()
|
||||
{
|
||||
if (paused_)
|
||||
{
|
||||
player_->resume();
|
||||
room_->resume();
|
||||
scoreboard_->resume();
|
||||
paused_ = false;
|
||||
}
|
||||
else
|
||||
{
|
||||
player_->pause();
|
||||
room_->pause();
|
||||
scoreboard_->pause();
|
||||
paused_ = true;
|
||||
}
|
||||
}
|
||||
|
||||
// Da vidas al jugador cuando está en la Jail
|
||||
void Game::checkRestoringJail()
|
||||
{
|
||||
if (room_->getName() != "THE JAIL" || board_.lives == 9)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
static int counter = 0;
|
||||
|
||||
if (!paused_)
|
||||
{
|
||||
counter++;
|
||||
}
|
||||
|
||||
// Incrementa el numero de vidas
|
||||
if (counter == 100)
|
||||
{
|
||||
counter = 0;
|
||||
board_.lives++;
|
||||
JA_PlaySound(death_sound_);
|
||||
|
||||
// Invalida el logro de completar el juego sin entrar a la jail
|
||||
const bool haveTheItems = board_.items >= int(total_items_ * 0.9f);
|
||||
if (!haveTheItems)
|
||||
{
|
||||
cheevos_->invalidate(9);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Inicializa el diccionario de las estadísticas
|
||||
void Game::initStats()
|
||||
{
|
||||
std::vector<res_room_t> *rooms = new std::vector<res_room_t>;
|
||||
rooms = resource_->getAllRooms();
|
||||
|
||||
for (auto room : *rooms)
|
||||
{
|
||||
stats_->addDictionary(room.room->number, room.room->name);
|
||||
}
|
||||
|
||||
stats_->init();
|
||||
}
|
||||
|
||||
// Crea la textura con el nombre de la habitación
|
||||
void Game::fillRoomNameTexture()
|
||||
{
|
||||
// Pone la textura como destino de renderizado
|
||||
SDL_SetRenderTarget(renderer_, room_name_texture_);
|
||||
|
||||
// Rellena la textura de color
|
||||
const color_t color = stringToColor(options.palette, "white");
|
||||
SDL_SetRenderDrawColor(renderer_, color.r, color.g, color.b, 0xFF);
|
||||
SDL_RenderClear(renderer_);
|
||||
|
||||
// Escribe el texto en la textura
|
||||
text_->writeDX(TXT_CENTER | TXT_COLOR, GAMECANVAS_CENTER_X, text_->getCharacterSize() / 2, room_->getName(), 1, room_->getBGColor());
|
||||
|
||||
// Deja el renderizador por defecto
|
||||
SDL_SetRenderTarget(renderer_, nullptr);
|
||||
}
|
||||
|
||||
// Comprueba algunos logros
|
||||
void Game::checkSomeCheevos()
|
||||
{
|
||||
// Logros sobre la cantidad de items
|
||||
if (board_.items == total_items_)
|
||||
{
|
||||
cheevos_->unlock(4);
|
||||
cheevos_->unlock(3);
|
||||
cheevos_->unlock(2);
|
||||
cheevos_->unlock(1);
|
||||
}
|
||||
else if (board_.items >= total_items_ * 0.75f)
|
||||
{
|
||||
cheevos_->unlock(3);
|
||||
cheevos_->unlock(2);
|
||||
cheevos_->unlock(1);
|
||||
}
|
||||
else if (board_.items >= total_items_ * 0.5f)
|
||||
{
|
||||
cheevos_->unlock(2);
|
||||
cheevos_->unlock(1);
|
||||
}
|
||||
else if (board_.items >= total_items_ * 0.25f)
|
||||
{
|
||||
cheevos_->unlock(1);
|
||||
}
|
||||
|
||||
// Logros sobre las habitaciones visitadas
|
||||
if (board_.rooms >= 60)
|
||||
{
|
||||
cheevos_->unlock(7);
|
||||
cheevos_->unlock(6);
|
||||
cheevos_->unlock(5);
|
||||
}
|
||||
else if (board_.rooms >= 40)
|
||||
{
|
||||
cheevos_->unlock(6);
|
||||
cheevos_->unlock(5);
|
||||
}
|
||||
else if (board_.rooms >= 20)
|
||||
{
|
||||
cheevos_->unlock(5);
|
||||
}
|
||||
}
|
||||
|
||||
// Comprueba los logros de completar el juego
|
||||
void Game::checkEndGameCheevos()
|
||||
{
|
||||
// "Complete the game"
|
||||
cheevos_->unlock(8);
|
||||
|
||||
// "Complete the game without entering the jail"
|
||||
cheevos_->unlock(9);
|
||||
|
||||
// "Complete the game with all items"
|
||||
if (board_.items == total_items_)
|
||||
{
|
||||
cheevos_->unlock(10);
|
||||
}
|
||||
|
||||
// "Complete the game without dying"
|
||||
cheevos_->unlock(11);
|
||||
|
||||
// "Complete the game in under 30 minutes"
|
||||
if (scoreboard_->getMinutes() < 30)
|
||||
{
|
||||
cheevos_->unlock(12);
|
||||
}
|
||||
}
|
||||
158
source/game.h
Normal file
158
source/game.h
Normal file
@@ -0,0 +1,158 @@
|
||||
#pragma once
|
||||
|
||||
#include <SDL2/SDL_events.h> // Para SDL_Event
|
||||
#include <SDL2/SDL_rect.h> // Para SDL_Rect
|
||||
#include <SDL2/SDL_render.h> // Para SDL_Renderer, SDL_Texture
|
||||
#include <SDL2/SDL_stdinc.h> // Para Uint32
|
||||
#include <string> // Para string, basic_string
|
||||
#include "player.h" // Para playerSpawn_t
|
||||
#include "scoreboard.h" // Para board_t
|
||||
class Asset;
|
||||
class Cheevos;
|
||||
class Debug;
|
||||
class Input;
|
||||
class ItemTracker;
|
||||
class Resource;
|
||||
class Room;
|
||||
class RoomTracker;
|
||||
class Screen;
|
||||
class Stats;
|
||||
class Text;
|
||||
struct JA_Music_t;
|
||||
struct JA_Sound_t;
|
||||
struct options_t;
|
||||
struct section_t;
|
||||
|
||||
class Game
|
||||
{
|
||||
private:
|
||||
// Objetos y punteros
|
||||
Screen *screen_; // Objeto encargado de manejar el renderizador
|
||||
SDL_Renderer *renderer_; // El renderizador de la ventana
|
||||
Room *room_; // Objeto encargado de gestionar cada habitación del juego
|
||||
Player *player_; // Objeto con el jugador
|
||||
ItemTracker *item_tracker_; // Lleva el control de los objetos recogidos
|
||||
RoomTracker *room_tracker_; // Lleva el control de las habitaciones visitadas
|
||||
Asset *asset_; // Objeto con la ruta a todos los ficheros de recursos
|
||||
Input *input_; // Objeto pata gestionar la entrada
|
||||
Text *text_; // Objeto para los textos del juego
|
||||
Scoreboard *scoreboard_; // Objeto encargado de gestionar el marcador
|
||||
Cheevos *cheevos_; // Objeto encargado de gestionar los logros del juego
|
||||
Resource *resource_; // Objeto con los recursos
|
||||
Debug *debug_; // Objeto para gestionar la información de debug
|
||||
Stats *stats_; // Objeto encargado de gestionar las estadísticas
|
||||
SDL_Texture *room_name_texture_; // Textura para escribir el nombre de la habitación
|
||||
|
||||
// Variables
|
||||
JA_Music_t *music_; // Musica que suena durante el juego
|
||||
Uint32 ticks_; // Contador de ticks para ajustar la velocidad del programa
|
||||
Uint32 ticks_speed_; // Velocidad a la que se repiten los bucles del programa
|
||||
std::string current_room_; // Fichero de la habitación actual
|
||||
playerSpawn_t spawn_point_; // Lugar de la habitación donde aparece el jugador
|
||||
JA_Sound_t *death_sound_; // Sonido a reproducir cuando muere el jugador
|
||||
board_t board_; // Estructura con los datos del marcador
|
||||
bool paused_; // Indica si el juego se encuentra en pausa
|
||||
bool black_screen_; // Indica si la pantalla está en negro. Se utiliza para la muerte del jugador
|
||||
int black_screen_counter_; // Contador para temporizar la pantalla en negro
|
||||
int total_items_; // Cantidad total de items que hay en el mapeado del juego
|
||||
SDL_Rect room_name_rect_; // Rectangulo donde pintar la textura con el nombre de la habitación
|
||||
|
||||
// Actualiza el juego, las variables, comprueba la entrada, etc.
|
||||
void update();
|
||||
|
||||
// Pinta los objetos en pantalla
|
||||
void render();
|
||||
|
||||
// Comprueba los eventos de la cola
|
||||
void checkEvents();
|
||||
|
||||
#ifdef DEBUG
|
||||
// Pone la información de debug en pantalla
|
||||
void updateDebugInfo();
|
||||
|
||||
// Pone la información de debug en pantalla
|
||||
void renderDebugInfo();
|
||||
#endif
|
||||
|
||||
// Escribe el nombre de la pantalla
|
||||
void renderRoomName();
|
||||
|
||||
// Cambia de habitación
|
||||
bool changeRoom(std::string file);
|
||||
|
||||
// Comprueba el teclado
|
||||
void checkInput();
|
||||
|
||||
// Comprueba si el jugador esta en el borde de la pantalla y actua
|
||||
void checkPlayerOnBorder();
|
||||
|
||||
// Comprueba las colisiones del jugador con los enemigos
|
||||
bool checkPlayerAndEnemies();
|
||||
|
||||
// Comprueba las colisiones del jugador con los objetos
|
||||
void checkPlayerAndItems();
|
||||
|
||||
// Comprueba si el jugador esta vivo
|
||||
void checkIfPlayerIsAlive();
|
||||
|
||||
// Comprueba si ha terminado la partida
|
||||
void checkGameOver();
|
||||
|
||||
// Mata al jugador
|
||||
void killPlayer();
|
||||
|
||||
// Recarga todas las texturas
|
||||
void reLoadTextures();
|
||||
|
||||
// Cambia la paleta
|
||||
void switchPalette();
|
||||
|
||||
// Establece la pantalla en negro
|
||||
void setBlackScreen();
|
||||
|
||||
// Actualiza las variables relativas a la pantalla en negro
|
||||
void updateBlackScreen();
|
||||
|
||||
// Dibuja la pantalla negra
|
||||
void renderBlackScreen();
|
||||
|
||||
// Pone el color del marcador en función del color del borde de la habitación
|
||||
void setScoreBoardColor();
|
||||
|
||||
// Comprueba si ha finalizado el juego
|
||||
bool checkEndGame();
|
||||
|
||||
// Obtiene la cantidad total de items que hay en el mapeado del juego
|
||||
int getTotalItems();
|
||||
|
||||
// Va a la habitación designada
|
||||
void goToRoom(int border);
|
||||
|
||||
// Pone el juego en pausa
|
||||
void switchPause();
|
||||
|
||||
// Da vidas al jugador cuando está en la Jail
|
||||
void checkRestoringJail();
|
||||
|
||||
// Inicializa el diccionario de las estadísticas
|
||||
void initStats();
|
||||
|
||||
// Pone el nombre de la habitación en la textura
|
||||
void fillRoomNameTexture();
|
||||
|
||||
// Comprueba algunos logros
|
||||
void checkSomeCheevos();
|
||||
|
||||
// Comprueba los logros de completar el juego
|
||||
void checkEndGameCheevos();
|
||||
|
||||
public:
|
||||
// Constructor
|
||||
Game();
|
||||
|
||||
// Destructor
|
||||
~Game();
|
||||
|
||||
// Bucle para el juego
|
||||
void run();
|
||||
};
|
||||
@@ -1,19 +1,27 @@
|
||||
#include "gamestate_game_over.h"
|
||||
#include "game_over.h"
|
||||
#include <SDL2/SDL_timer.h> // Para SDL_GetTicks
|
||||
#include <algorithm> // Para min, max
|
||||
#include <string> // Para basic_string, operator+, to_string, char...
|
||||
#include "animatedsprite.h" // Para AnimatedSprite
|
||||
#include "asset.h" // Para Asset
|
||||
#include "const.h" // Para GAMECANVAS_CENTER_X, SECTION_GAME_OVER
|
||||
#include "input.h" // Para Input, REPEAT_FALSE, inputs_e
|
||||
#include "jail_audio.h" // Para JA_DeleteMusic, JA_LoadMusic, JA_PlayMusic
|
||||
#include "resource.h" // Para Resource
|
||||
#include "screen.h" // Para Screen
|
||||
#include "text.h" // Para Text, TXT_CENTER, TXT_COLOR
|
||||
#include "texture.h" // Para Texture
|
||||
#include "options.h"
|
||||
|
||||
// Constructor
|
||||
GameOver::GameOver(SDL_Renderer *renderer, Screen *screen, Resource *resource, Asset *asset, Input *input, options_t *options, section_t *section)
|
||||
GameOver::GameOver()
|
||||
: screen(Screen::get()),
|
||||
renderer(Screen::get()->getRenderer()),
|
||||
resource(Resource::get()),
|
||||
asset(Asset::get()),
|
||||
input(Input::get())
|
||||
{
|
||||
// Copia los punteros
|
||||
this->renderer = renderer;
|
||||
this->screen = screen;
|
||||
this->resource = resource;
|
||||
this->asset = asset;
|
||||
this->input = input;
|
||||
this->options = options;
|
||||
this->section = section;
|
||||
|
||||
// Reserva memoria para los punteros a objetos
|
||||
eventHandler = new SDL_Event();
|
||||
text = new Text(resource->getOffset("smb2.txt"), resource->getTexture("smb2.png"), renderer);
|
||||
playerSprite = new AnimatedSprite(renderer, resource->getAnimation("player_game_over.ani"));
|
||||
tvSprite = new AnimatedSprite(renderer, resource->getAnimation("tv.ani"));
|
||||
@@ -22,8 +30,8 @@ GameOver::GameOver(SDL_Renderer *renderer, Screen *screen, Resource *resource, A
|
||||
// Inicializa variables
|
||||
preCounter = 0;
|
||||
counter = 0;
|
||||
section->name = SECTION_GAME_OVER;
|
||||
section->subsection = 0;
|
||||
options.section.name = SECTION_GAME_OVER;
|
||||
options.section.subsection = 0;
|
||||
ticks = 0;
|
||||
ticksSpeed = 15;
|
||||
endSection = 400;
|
||||
@@ -38,7 +46,7 @@ GameOver::GameOver(SDL_Renderer *renderer, Screen *screen, Resource *resource, A
|
||||
const std::vector<std::string> colorList = {"white", "yellow", "cyan", "green", "magenta", "red", "blue", "black"};
|
||||
for (auto cl : colorList)
|
||||
{
|
||||
colors.push_back(stringToColor(options->palette, cl));
|
||||
colors.push_back(stringToColor(options.palette, cl));
|
||||
}
|
||||
color = colors.back();
|
||||
}
|
||||
@@ -47,7 +55,6 @@ GameOver::GameOver(SDL_Renderer *renderer, Screen *screen, Resource *resource, A
|
||||
GameOver::~GameOver()
|
||||
{
|
||||
// Libera la memoria de los objetos
|
||||
delete eventHandler;
|
||||
delete text;
|
||||
delete playerSprite;
|
||||
delete tvSprite;
|
||||
@@ -76,8 +83,7 @@ void GameOver::update()
|
||||
playerSprite->update();
|
||||
tvSprite->update();
|
||||
|
||||
// Actualiza las notificaciones
|
||||
screen->updateNotifier();
|
||||
screen->update();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -101,30 +107,30 @@ void GameOver::render()
|
||||
renderSprites();
|
||||
|
||||
// Escribe el texto con las habitaciones y los items
|
||||
const std::string itemsTxt = std::to_string(options->stats.items / 100) + std::to_string((options->stats.items % 100) / 10) + std::to_string(options->stats.items % 10);
|
||||
const std::string roomsTxt = std::to_string(options->stats.rooms / 100) + std::to_string((options->stats.rooms % 100) / 10) + std::to_string(options->stats.rooms % 10);
|
||||
const std::string itemsTxt = std::to_string(options.stats.items / 100) + std::to_string((options.stats.items % 100) / 10) + std::to_string(options.stats.items % 10);
|
||||
const std::string roomsTxt = std::to_string(options.stats.rooms / 100) + std::to_string((options.stats.rooms % 100) / 10) + std::to_string(options.stats.rooms % 10);
|
||||
text->writeDX(TXT_CENTER | TXT_COLOR, GAMECANVAS_CENTER_X, y + 80, "ITEMS: " + itemsTxt, 1, color);
|
||||
text->writeDX(TXT_CENTER | TXT_COLOR, GAMECANVAS_CENTER_X, y + 90, "ROOMS: " + roomsTxt, 1, color);
|
||||
|
||||
// Escribe el texto con "Tu peor pesadilla"
|
||||
text->writeDX(TXT_CENTER | TXT_COLOR, GAMECANVAS_CENTER_X, y + 110, "YOUR WORST NIGHTMARE IS", 1, color);
|
||||
text->writeDX(TXT_CENTER | TXT_COLOR, GAMECANVAS_CENTER_X, y + 120, options->stats.worstNightmare, 1, color);
|
||||
text->writeDX(TXT_CENTER | TXT_COLOR, GAMECANVAS_CENTER_X, y + 120, options.stats.worstNightmare, 1, color);
|
||||
|
||||
// Vuelca el contenido del renderizador en pantalla
|
||||
screen->blit();
|
||||
screen->render();
|
||||
}
|
||||
|
||||
// Comprueba el manejador de eventos
|
||||
void GameOver::checkEvents()
|
||||
{
|
||||
// Comprueba los eventos que hay en la cola
|
||||
while (SDL_PollEvent(eventHandler) != 0)
|
||||
SDL_Event event;
|
||||
while (SDL_PollEvent(&event))
|
||||
{
|
||||
// Evento de salida de la aplicación
|
||||
if (eventHandler->type == SDL_QUIT)
|
||||
if (event.type == SDL_QUIT)
|
||||
{
|
||||
section->name = SECTION_QUIT;
|
||||
section->subsection = 0;
|
||||
options.section.name = SECTION_QUIT;
|
||||
options.section.subsection = 0;
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -136,17 +142,17 @@ void GameOver::checkInput()
|
||||
|
||||
if (input->checkInput(input_exit, REPEAT_FALSE))
|
||||
{
|
||||
section->name = SECTION_QUIT;
|
||||
options.section.name = SECTION_QUIT;
|
||||
}
|
||||
|
||||
else if (input->checkInput(input_toggle_border, REPEAT_FALSE))
|
||||
{
|
||||
screen->switchBorder();
|
||||
screen->toggleBorder();
|
||||
}
|
||||
|
||||
else if (input->checkInput(input_window_fullscreen, REPEAT_FALSE))
|
||||
else if (input->checkInput(input_toggle_videomode, REPEAT_FALSE))
|
||||
{
|
||||
screen->switchVideoMode();
|
||||
screen->toggleVideoMode();
|
||||
}
|
||||
|
||||
else if (input->checkInput(input_window_dec_size, REPEAT_FALSE))
|
||||
@@ -159,7 +165,7 @@ void GameOver::checkInput()
|
||||
screen->incWindowSize();
|
||||
}
|
||||
|
||||
else if (input->checkInput(input_swap_palette, REPEAT_FALSE))
|
||||
else if (input->checkInput(input_toggle_palette, REPEAT_FALSE))
|
||||
{
|
||||
switchPalette();
|
||||
}
|
||||
@@ -168,7 +174,7 @@ void GameOver::checkInput()
|
||||
// Bucle principal
|
||||
void GameOver::run()
|
||||
{
|
||||
while (section->name == SECTION_GAME_OVER)
|
||||
while (options.section.name == SECTION_GAME_OVER)
|
||||
{
|
||||
update();
|
||||
checkEvents();
|
||||
@@ -227,13 +233,13 @@ void GameOver::updateCounters()
|
||||
// Comprueba si ha terminado la sección
|
||||
else if (counter == endSection)
|
||||
{
|
||||
section->name = SECTION_LOGO;
|
||||
section->subsection = SUBSECTION_LOGO_TO_TITLE;
|
||||
options.section.name = SECTION_LOGO;
|
||||
options.section.subsection = SUBSECTION_LOGO_TO_TITLE;
|
||||
}
|
||||
}
|
||||
|
||||
// Cambia la paleta
|
||||
void GameOver::switchPalette()
|
||||
{
|
||||
options->palette = (options->palette == p_zxspectrum) ? p_zxarne : p_zxspectrum;
|
||||
options.palette = (options.palette == p_zxspectrum) ? p_zxarne : p_zxspectrum;
|
||||
}
|
||||
@@ -1,36 +1,30 @@
|
||||
#pragma once
|
||||
#include <SDL2/SDL.h>
|
||||
#include "jail_engine/animatedsprite.h"
|
||||
#include "jail_engine/asset.h"
|
||||
#include "jail_engine/input.h"
|
||||
#include "jail_engine/jail_audio.h"
|
||||
#include "jail_engine/resource.h"
|
||||
#include "jail_engine/screen.h"
|
||||
#include "jail_engine/sprite.h"
|
||||
#include "jail_engine/text.h"
|
||||
#include "jail_engine/texture.h"
|
||||
#include "jail_engine/utils.h"
|
||||
#include "const.h"
|
||||
#include <vector>
|
||||
|
||||
#ifndef GAME_OVER_H
|
||||
#define GAME_OVER_H
|
||||
#include <SDL2/SDL_events.h> // Para SDL_Event
|
||||
#include <SDL2/SDL_render.h> // Para SDL_Renderer
|
||||
#include <SDL2/SDL_stdinc.h> // Para Uint32
|
||||
#include <vector> // Para vector
|
||||
#include "utils.h" // Para color_t
|
||||
class AnimatedSprite;
|
||||
class Asset;
|
||||
class Input;
|
||||
class Resource;
|
||||
class Screen;
|
||||
class Text;
|
||||
struct JA_Music_t;
|
||||
|
||||
class GameOver
|
||||
{
|
||||
private:
|
||||
// Objetos y punteros
|
||||
SDL_Renderer *renderer; // El renderizador de la ventana
|
||||
Screen *screen; // Objeto encargado de dibujar en pantalla
|
||||
SDL_Renderer *renderer; // El renderizador de la ventana
|
||||
Resource *resource; // Objeto con los recursos
|
||||
Asset *asset; // Objeto con los ficheros de recursos
|
||||
Input *input; // Objeto pata gestionar la entrada
|
||||
options_t *options; // Puntero a las opciones del juego
|
||||
SDL_Event *eventHandler; // Manejador de eventos
|
||||
Text *text; // Objeto para escribir texto en pantalla
|
||||
AnimatedSprite *playerSprite; // Sprite con el jugador
|
||||
AnimatedSprite *tvSprite; // Sprite con el televisor
|
||||
section_t *section; // Estado del bucle principal para saber si continua o se sale
|
||||
|
||||
// Variables
|
||||
int preCounter; // Contador previo
|
||||
@@ -70,13 +64,11 @@ private:
|
||||
|
||||
public:
|
||||
// Constructor
|
||||
GameOver(SDL_Renderer *renderer, Screen *screen, Resource *resource, Asset *asset, Input *input, options_t *options, section_t *section);
|
||||
GameOver();
|
||||
|
||||
// Destructor
|
||||
~GameOver();
|
||||
|
||||
// Bucle principal
|
||||
void run();
|
||||
};
|
||||
|
||||
#endif
|
||||
};
|
||||
@@ -1,360 +0,0 @@
|
||||
#include "gamestate_credits.h"
|
||||
#include <iostream>
|
||||
|
||||
// Constructor
|
||||
Credits::Credits(SDL_Renderer *renderer, Screen *screen, Resource *resource, Asset *asset, Input *input, options_t *options, section_t *section)
|
||||
{
|
||||
// Copia la dirección de los objetos
|
||||
this->renderer = renderer;
|
||||
this->screen = screen;
|
||||
this->resource = resource;
|
||||
this->asset = asset;
|
||||
this->input = input;
|
||||
this->options = options;
|
||||
this->section = section;
|
||||
|
||||
// Reserva memoria para los punteros
|
||||
eventHandler = new SDL_Event();
|
||||
text = new Text(resource->getOffset("smb2.txt"), resource->getTexture("smb2.png"), renderer);
|
||||
sprite = new AnimatedSprite(renderer, resource->getAnimation("shine.ani"));
|
||||
|
||||
// Inicializa variables
|
||||
counter = 0;
|
||||
counterEnabled = true;
|
||||
subCounter = 0;
|
||||
section->name = SECTION_CREDITS;
|
||||
section->subsection = 0;
|
||||
ticks = 0;
|
||||
ticksSpeed = 15;
|
||||
sprite->setRect({194, 174, 8, 8});
|
||||
|
||||
// Cambia el color del borde
|
||||
screen->setBorderColor(stringToColor(options->palette, "black"));
|
||||
|
||||
// Crea la textura para el texto que se escribe en pantalla
|
||||
textTexture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, GAMECANVAS_WIDTH, GAMECANVAS_HEIGHT);
|
||||
if (textTexture == nullptr)
|
||||
{
|
||||
if (options->console)
|
||||
{
|
||||
std::cout << "Error: textTexture could not be created!\nSDL Error: " << SDL_GetError() << std::endl;
|
||||
}
|
||||
}
|
||||
SDL_SetTextureBlendMode(textTexture, SDL_BLENDMODE_BLEND);
|
||||
|
||||
// Crea la textura para cubrir el rexto
|
||||
coverTexture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, GAMECANVAS_WIDTH, GAMECANVAS_HEIGHT);
|
||||
if (coverTexture == nullptr)
|
||||
{
|
||||
if (options->console)
|
||||
{
|
||||
std::cout << "Error: textTexture could not be created!\nSDL Error: " << SDL_GetError() << std::endl;
|
||||
}
|
||||
}
|
||||
SDL_SetTextureBlendMode(coverTexture, SDL_BLENDMODE_BLEND);
|
||||
|
||||
// Escribe el texto en la textura
|
||||
fillTexture();
|
||||
}
|
||||
|
||||
// Destructor
|
||||
Credits::~Credits()
|
||||
{
|
||||
delete eventHandler;
|
||||
delete text;
|
||||
delete sprite;
|
||||
SDL_DestroyTexture(textTexture);
|
||||
SDL_DestroyTexture(coverTexture);
|
||||
}
|
||||
|
||||
// Comprueba el manejador de eventos
|
||||
void Credits::checkEvents()
|
||||
{
|
||||
// Comprueba los eventos que hay en la cola
|
||||
while (SDL_PollEvent(eventHandler) != 0)
|
||||
{
|
||||
// Evento de salida de la aplicación
|
||||
if (eventHandler->type == SDL_QUIT)
|
||||
{
|
||||
section->name = SECTION_QUIT;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Comprueba las entradas
|
||||
void Credits::checkInput()
|
||||
{
|
||||
|
||||
if (input->checkInput(input_exit, REPEAT_FALSE))
|
||||
{
|
||||
section->name = SECTION_QUIT;
|
||||
}
|
||||
|
||||
else if (input->checkInput(input_toggle_border, REPEAT_FALSE))
|
||||
{
|
||||
screen->switchBorder();
|
||||
}
|
||||
|
||||
else if (input->checkInput(input_window_fullscreen, REPEAT_FALSE))
|
||||
{
|
||||
screen->switchVideoMode();
|
||||
}
|
||||
|
||||
else if (input->checkInput(input_window_dec_size, REPEAT_FALSE))
|
||||
{
|
||||
screen->decWindowSize();
|
||||
}
|
||||
|
||||
else if (input->checkInput(input_window_inc_size, REPEAT_FALSE))
|
||||
{
|
||||
screen->incWindowSize();
|
||||
}
|
||||
|
||||
else if (input->checkInput(input_swap_palette, REPEAT_FALSE))
|
||||
{
|
||||
switchPalette();
|
||||
}
|
||||
|
||||
else if (input->checkInput(input_pause, REPEAT_FALSE) || input->checkInput(input_accept, REPEAT_FALSE) || input->checkInput(input_jump, REPEAT_FALSE))
|
||||
{
|
||||
section->name = SECTION_TITLE;
|
||||
section->subsection = 0;
|
||||
}
|
||||
}
|
||||
|
||||
// Inicializa los textos
|
||||
void Credits::iniTexts()
|
||||
{
|
||||
std::string keys = "";
|
||||
if (options->keys == ctrl_cursor)
|
||||
{
|
||||
keys = "CURSORS";
|
||||
}
|
||||
else if (options->keys == ctrl_opqa)
|
||||
{
|
||||
keys = "O,P AND Q";
|
||||
}
|
||||
else
|
||||
{
|
||||
keys = "A,D AND W";
|
||||
}
|
||||
|
||||
#ifndef GAME_CONSOLE
|
||||
texts.clear();
|
||||
texts.push_back({"", stringToColor(options->palette, "white")});
|
||||
texts.push_back({"INSTRUCTIONS:", stringToColor(options->palette, "yellow")});
|
||||
texts.push_back({"", stringToColor(options->palette, "white")});
|
||||
texts.push_back({"HELP JAILDOC TO GET BACK ALL", stringToColor(options->palette, "white")});
|
||||
texts.push_back({"HIS PROJECTS AND GO TO THE", stringToColor(options->palette, "white")});
|
||||
texts.push_back({"JAIL TO FINISH THEM", stringToColor(options->palette, "white")});
|
||||
texts.push_back({"", stringToColor(options->palette, "white")});
|
||||
texts.push_back({"", stringToColor(options->palette, "white")});
|
||||
|
||||
texts.push_back({"KEYS:", stringToColor(options->palette, "yellow")});
|
||||
texts.push_back({"", stringToColor(options->palette, "white")});
|
||||
texts.push_back({keys + " TO MOVE AND JUMP", stringToColor(options->palette, "white")});
|
||||
texts.push_back({"M TO SWITCH THE MUSIC", stringToColor(options->palette, "white")});
|
||||
texts.push_back({"H TO PAUSE THE GAME", stringToColor(options->palette, "white")});
|
||||
texts.push_back({"F1-F2 TO CHANGE WINDOWS SIZE", stringToColor(options->palette, "white")});
|
||||
texts.push_back({"F3 TO SWITCH TO FULLSCREEN", stringToColor(options->palette, "white")});
|
||||
texts.push_back({"B TO TOOGLE THE BORDER SCREEN", stringToColor(options->palette, "white")});
|
||||
texts.push_back({"", stringToColor(options->palette, "white")});
|
||||
texts.push_back({"", stringToColor(options->palette, "white")});
|
||||
|
||||
texts.push_back({"A GAME BY JAILDESIGNER", stringToColor(options->palette, "yellow")});
|
||||
texts.push_back({"MADE ON SUMMER/FALL 2022", stringToColor(options->palette, "yellow")});
|
||||
texts.push_back({"", stringToColor(options->palette, "white")});
|
||||
texts.push_back({"", stringToColor(options->palette, "white")});
|
||||
|
||||
texts.push_back({"I LOVE JAILGAMES! ", stringToColor(options->palette, "white")});
|
||||
texts.push_back({"", stringToColor(options->palette, "white")});
|
||||
#else
|
||||
texts.clear();
|
||||
texts.push_back({"", stringToColor(options->palette, "white")});
|
||||
texts.push_back({"INSTRUCTIONS:", stringToColor(options->palette, "yellow")});
|
||||
texts.push_back({"", stringToColor(options->palette, "white")});
|
||||
texts.push_back({"HELP JAILDOC TO GET BACK ALL", stringToColor(options->palette, "white")});
|
||||
texts.push_back({"HIS PROJECTS AND GO TO THE", stringToColor(options->palette, "white")});
|
||||
texts.push_back({"JAIL TO FINISH THEM", stringToColor(options->palette, "white")});
|
||||
texts.push_back({"", stringToColor(options->palette, "white")});
|
||||
texts.push_back({"", stringToColor(options->palette, "white")});
|
||||
|
||||
texts.push_back({"KEYS:", stringToColor(options->palette, "yellow")});
|
||||
texts.push_back({"", stringToColor(options->palette, "white")});
|
||||
texts.push_back({"B TO JUMP", stringToColor(options->palette, "white")});
|
||||
texts.push_back({"R TO SWITCH THE MUSIC", stringToColor(options->palette, "white")});
|
||||
texts.push_back({"L TO SWAP THE COLOR PALETTE", stringToColor(options->palette, "white")});
|
||||
texts.push_back({"START TO PAUSE", stringToColor(options->palette, "white")});
|
||||
texts.push_back({"SELECT TO EXIT", stringToColor(options->palette, "white")});
|
||||
texts.push_back({"", stringToColor(options->palette, "white")});
|
||||
texts.push_back({"", stringToColor(options->palette, "white")});
|
||||
texts.push_back({"", stringToColor(options->palette, "white")});
|
||||
|
||||
texts.push_back({"A GAME BY JAILDESIGNER", stringToColor(options->palette, "yellow")});
|
||||
texts.push_back({"MADE ON SUMMER/FALL 2022", stringToColor(options->palette, "yellow")});
|
||||
texts.push_back({"", stringToColor(options->palette, "white")});
|
||||
texts.push_back({"", stringToColor(options->palette, "white")});
|
||||
|
||||
texts.push_back({"I LOVE JAILGAMES! ", stringToColor(options->palette, "white")});
|
||||
texts.push_back({"", stringToColor(options->palette, "white")});
|
||||
#endif
|
||||
}
|
||||
|
||||
// Escribe el texto en la textura
|
||||
void Credits::fillTexture()
|
||||
{
|
||||
// Inicializa los textos
|
||||
iniTexts();
|
||||
|
||||
// Rellena la textura de texto
|
||||
SDL_SetRenderTarget(renderer, textTexture);
|
||||
color_t c = stringToColor(options->palette, "black");
|
||||
SDL_SetRenderDrawColor(renderer, c.r, c.g, c.b, 0xFF);
|
||||
SDL_RenderClear(renderer);
|
||||
|
||||
// Escribe el texto en la textura
|
||||
const int size = text->getCharacterSize();
|
||||
int i = 0;
|
||||
|
||||
for (auto t : texts)
|
||||
{
|
||||
text->writeDX(TXT_CENTER | TXT_COLOR, PLAY_AREA_CENTER_X, i * size, t.label, 1, t.color);
|
||||
i++;
|
||||
}
|
||||
|
||||
// Escribe el corazón
|
||||
const int textLenght = text->lenght(texts[22].label, 1) - text->lenght(" ", 1); // Se resta el ultimo caracter que es un espacio
|
||||
const int posX = ((PLAY_AREA_WIDTH - textLenght) / 2) + textLenght;
|
||||
text->writeColored(posX, 176, "}", stringToColor(options->palette, "bright_red"));
|
||||
|
||||
// Recoloca el sprite del brillo
|
||||
sprite->setPosX(posX + 2);
|
||||
|
||||
SDL_SetRenderTarget(renderer, nullptr);
|
||||
|
||||
// Rellena la textura que cubre el texto con color transparente
|
||||
SDL_SetRenderTarget(renderer, coverTexture);
|
||||
SDL_SetRenderDrawColor(renderer, c.r, c.g, c.b, 0x00);
|
||||
SDL_RenderClear(renderer);
|
||||
|
||||
// Los primeros 8 pixels crea una malla
|
||||
SDL_SetRenderDrawColor(renderer, c.r, c.g, c.b, 0xFF);
|
||||
for (int i = 0; i < 256; i += 2)
|
||||
{
|
||||
SDL_RenderDrawPoint(renderer, i, 0);
|
||||
SDL_RenderDrawPoint(renderer, i, 2);
|
||||
SDL_RenderDrawPoint(renderer, i, 4);
|
||||
SDL_RenderDrawPoint(renderer, i, 6);
|
||||
|
||||
SDL_RenderDrawPoint(renderer, i + 1, 5);
|
||||
SDL_RenderDrawPoint(renderer, i + 1, 7);
|
||||
}
|
||||
|
||||
// El resto se rellena de color sólido
|
||||
SDL_Rect rect = {0, 8, 256, 192};
|
||||
SDL_RenderFillRect(renderer, &rect);
|
||||
|
||||
SDL_SetRenderTarget(renderer, nullptr);
|
||||
}
|
||||
|
||||
// Actualiza el contador
|
||||
void Credits::updateCounter()
|
||||
{
|
||||
// Incrementa el contador
|
||||
if (counterEnabled)
|
||||
{
|
||||
counter++;
|
||||
if (counter == 224 || counter == 544 || counter == 672)
|
||||
{
|
||||
counterEnabled = false;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
subCounter++;
|
||||
if (subCounter == 100)
|
||||
{
|
||||
counterEnabled = true;
|
||||
subCounter = 0;
|
||||
}
|
||||
}
|
||||
|
||||
// Comprueba si ha terminado la sección
|
||||
if (counter > 1200)
|
||||
{
|
||||
section->name = SECTION_DEMO;
|
||||
}
|
||||
}
|
||||
|
||||
// Actualiza las variables
|
||||
void Credits::update()
|
||||
{
|
||||
// Comprueba que la diferencia de ticks sea mayor a la velocidad del juego
|
||||
if (SDL_GetTicks() - ticks > ticksSpeed)
|
||||
{
|
||||
// Actualiza el contador de ticks
|
||||
ticks = SDL_GetTicks();
|
||||
|
||||
// Comprueba las entradas
|
||||
checkInput();
|
||||
|
||||
// Actualiza el contador
|
||||
updateCounter();
|
||||
|
||||
// Actualiza las notificaciones
|
||||
screen->updateNotifier();
|
||||
|
||||
// Actualiza el sprite con el brillo
|
||||
if (counter > 770)
|
||||
{
|
||||
sprite->update();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Dibuja en pantalla
|
||||
void Credits::render()
|
||||
{
|
||||
// Prepara para empezar a dibujar en la textura de juego
|
||||
screen->start();
|
||||
|
||||
// Limpia la pantalla
|
||||
screen->clean();
|
||||
|
||||
if (counter < 1150)
|
||||
{
|
||||
// Dibuja la textura con el texto en pantalla
|
||||
SDL_RenderCopy(renderer, textTexture, nullptr, nullptr);
|
||||
|
||||
// Dibuja la textura que cubre el texto
|
||||
const int offset = std::min(counter / 8, 192 / 2);
|
||||
SDL_Rect srcRect = {0, 0, 256, 192 - (offset * 2)};
|
||||
SDL_Rect dstRect = {0, offset * 2, 256, 192 - (offset * 2)};
|
||||
SDL_RenderCopy(renderer, coverTexture, &srcRect, &dstRect);
|
||||
|
||||
// Dibuja el sprite con el brillo
|
||||
sprite->render();
|
||||
}
|
||||
|
||||
// Vuelca el contenido del renderizador en pantalla
|
||||
screen->blit();
|
||||
}
|
||||
|
||||
// Bucle para el logo del juego
|
||||
void Credits::run()
|
||||
{
|
||||
while (section->name == SECTION_CREDITS)
|
||||
{
|
||||
update();
|
||||
checkEvents();
|
||||
render();
|
||||
}
|
||||
}
|
||||
|
||||
// Cambia la paleta
|
||||
void Credits::switchPalette()
|
||||
{
|
||||
options->palette = options->palette == p_zxspectrum ? p_zxarne : p_zxspectrum;
|
||||
fillTexture();
|
||||
}
|
||||
@@ -1,86 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include <SDL2/SDL.h>
|
||||
#include "jail_engine/animatedsprite.h"
|
||||
#include "jail_engine/asset.h"
|
||||
#include "jail_engine/input.h"
|
||||
#include "jail_engine/jail_audio.h"
|
||||
#include "jail_engine/resource.h"
|
||||
#include "jail_engine/screen.h"
|
||||
#include "jail_engine/sprite.h"
|
||||
#include "jail_engine/text.h"
|
||||
#include "jail_engine/texture.h"
|
||||
#include "jail_engine/utils.h"
|
||||
#include "const.h"
|
||||
#include <vector>
|
||||
|
||||
#ifndef CREDITS_H
|
||||
#define CREDITS_H
|
||||
|
||||
class Credits
|
||||
{
|
||||
private:
|
||||
struct captions_t
|
||||
{
|
||||
std::string label; // Texto a escribir
|
||||
color_t color; // Color del texto
|
||||
};
|
||||
|
||||
// Objetos y punteros
|
||||
SDL_Renderer *renderer; // El renderizador de la ventana
|
||||
Screen *screen; // Objeto encargado de dibujar en pantalla
|
||||
Resource *resource; // Objeto con los recursos
|
||||
Asset *asset; // Objeto con los ficheros de recursos
|
||||
Input *input; // Objeto pata gestionar la entrada
|
||||
SDL_Event *eventHandler; // Manejador de eventos
|
||||
Text *text; // Objeto para escribir texto en pantalla
|
||||
SDL_Texture *textTexture; // Textura para dibujar el texto
|
||||
SDL_Texture *coverTexture; // Textura para cubrir el texto
|
||||
AnimatedSprite *sprite; // Sprite para el brillo del corazón
|
||||
options_t *options; // Puntero a las opciones del juego
|
||||
section_t *section; // Estado del bucle principal para saber si continua o se sale
|
||||
|
||||
// Variables
|
||||
int counter; // Contador
|
||||
bool counterEnabled; // Indica si esta activo el contador
|
||||
int subCounter; // Contador secundario
|
||||
Uint32 ticks; // Contador de ticks para ajustar la velocidad del programa
|
||||
Uint32 ticksSpeed; // Velocidad a la que se repiten los bucles del programa
|
||||
std::vector<captions_t> texts; // Vector con los textos
|
||||
|
||||
// Actualiza las variables
|
||||
void update();
|
||||
|
||||
// Dibuja en pantalla
|
||||
void render();
|
||||
|
||||
// Comprueba el manejador de eventos
|
||||
void checkEvents();
|
||||
|
||||
// Comprueba las entradas
|
||||
void checkInput();
|
||||
|
||||
// Actualiza el contador
|
||||
void updateCounter();
|
||||
|
||||
// Inicializa los textos
|
||||
void iniTexts();
|
||||
|
||||
// Escribe el texto en la textura
|
||||
void fillTexture();
|
||||
|
||||
// Cambia la paleta
|
||||
void switchPalette();
|
||||
|
||||
public:
|
||||
// Constructor
|
||||
Credits(SDL_Renderer *renderer, Screen *screen, Resource *resource, Asset *asset, Input *input, options_t *options, section_t *section);
|
||||
|
||||
// Destructor
|
||||
~Credits();
|
||||
|
||||
// Bucle principal
|
||||
void run();
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -1,781 +0,0 @@
|
||||
#include "gamestate_game.h"
|
||||
#include <iostream>
|
||||
|
||||
// Constructor
|
||||
Game::Game(SDL_Renderer *renderer, Screen *screen, Resource *resource, Asset *asset, Online *online, options_t *options, Input *input, section_t *section, Debug *debug)
|
||||
{
|
||||
// Copia los punteros
|
||||
this->resource = resource;
|
||||
this->renderer = renderer;
|
||||
this->asset = asset;
|
||||
this->online = online;
|
||||
this->screen = screen;
|
||||
this->input = input;
|
||||
this->debug = debug;
|
||||
this->options = options;
|
||||
this->section = section;
|
||||
|
||||
// Inicia algunas variables
|
||||
board.iniClock = SDL_GetTicks();
|
||||
#ifdef DEBUG
|
||||
currentRoom = "03.room";
|
||||
const int x = 25;
|
||||
const int y = 13;
|
||||
spawnPoint = {x * 8, y * 8, 0, 0, 0, s_standing, SDL_FLIP_HORIZONTAL};
|
||||
debug->setEnabled(false);
|
||||
#else
|
||||
currentRoom = "03.room";
|
||||
const int x = 25;
|
||||
const int y = 13;
|
||||
spawnPoint = {x * 8, y * 8, 0, 0, 0, s_standing, SDL_FLIP_HORIZONTAL};
|
||||
#endif
|
||||
|
||||
// Crea los objetos
|
||||
cheevos = new Cheevos(screen, options, asset->get("cheevos.bin"), online);
|
||||
scoreboard = new ScoreBoard(renderer, resource, asset, options, &board);
|
||||
itemTracker = new ItemTracker();
|
||||
roomTracker = new RoomTracker();
|
||||
room = new Room(resource->getRoom(currentRoom), renderer, screen, asset, options, itemTracker, &board.items, false, debug);
|
||||
const std::string playerPNG = options->cheat.altSkin ? "player2.png" : "player.png";
|
||||
const std::string playerANI = options->cheat.altSkin ? "player2.ani" : "player.ani";
|
||||
const player_t player = {spawnPoint, playerPNG, playerANI, renderer, resource, asset, options, input, room, debug};
|
||||
this->player = new Player(player);
|
||||
eventHandler = new SDL_Event();
|
||||
text = new Text(resource->getOffset("smb2.txt"), resource->getTexture("smb2.png"), renderer);
|
||||
music = JA_LoadMusic(asset->get("game.ogg").c_str());
|
||||
deathSound = JA_LoadSound(asset->get("death.wav").c_str());
|
||||
stats = new Stats(asset->get("stats.csv"), asset->get("stats_buffer.csv"), options, online);
|
||||
|
||||
// Crea la textura para poner el nombre de la habitación
|
||||
roomNameTexture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, GAMECANVAS_WIDTH, text->getCharacterSize() * 2);
|
||||
if (roomNameTexture == nullptr)
|
||||
{
|
||||
if (options->console)
|
||||
{
|
||||
std::cout << "Error: roomNameTexture could not be created!\nSDL Error: " << SDL_GetError() << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
// Establece el blend mode de la textura
|
||||
SDL_SetTextureBlendMode(roomNameTexture, SDL_BLENDMODE_BLEND);
|
||||
|
||||
// Establece el destino de la textura
|
||||
roomNameRect = {0, PLAY_AREA_HEIGHT, GAMECANVAS_WIDTH, text->getCharacterSize() * 2};
|
||||
|
||||
// Pone el nombre de la habitación en la textura
|
||||
fillRoomNameTexture();
|
||||
|
||||
// Inicializa el resto de variables
|
||||
ticks = 0;
|
||||
ticksSpeed = 15;
|
||||
board.lives = 9;
|
||||
#ifdef DEBUG
|
||||
board.lives = 9;
|
||||
#endif
|
||||
board.items = 0;
|
||||
board.rooms = 1;
|
||||
board.music = true;
|
||||
board.jailEnabled = options->cheat.jailEnabled;
|
||||
setScoreBoardColor();
|
||||
roomTracker->addRoom(currentRoom);
|
||||
paused = false;
|
||||
blackScreen = false;
|
||||
blackScreenCounter = 0;
|
||||
totalItems = getTotalItems();
|
||||
initStats();
|
||||
stats->addVisit(room->getName());
|
||||
const bool cheats = options->cheat.infiniteLives || options->cheat.invincible || options->cheat.jailEnabled;
|
||||
cheevos->enable(!cheats); // Deshabilita los logros si hay trucos activados
|
||||
|
||||
section->name = SECTION_GAME;
|
||||
section->subsection = 0;
|
||||
}
|
||||
|
||||
Game::~Game()
|
||||
{
|
||||
// Libera la memoria de los objetos
|
||||
delete cheevos;
|
||||
delete scoreboard;
|
||||
delete itemTracker;
|
||||
delete roomTracker;
|
||||
delete room;
|
||||
delete player;
|
||||
delete eventHandler;
|
||||
delete text;
|
||||
delete stats;
|
||||
|
||||
SDL_DestroyTexture(roomNameTexture);
|
||||
|
||||
JA_DeleteMusic(music);
|
||||
JA_DeleteSound(deathSound);
|
||||
}
|
||||
|
||||
// Comprueba los eventos de la cola
|
||||
void Game::checkEvents()
|
||||
{
|
||||
// Comprueba los eventos que hay en la cola
|
||||
while (SDL_PollEvent(eventHandler) != 0)
|
||||
{
|
||||
// Evento de salida de la aplicación
|
||||
if (eventHandler->type == SDL_QUIT)
|
||||
{
|
||||
section->name = SECTION_QUIT;
|
||||
screen->setBorderColor(stringToColor(options->palette, "black"));
|
||||
break;
|
||||
}
|
||||
|
||||
if (eventHandler->type == SDL_RENDER_DEVICE_RESET || eventHandler->type == SDL_RENDER_TARGETS_RESET)
|
||||
{
|
||||
reLoadTextures();
|
||||
}
|
||||
|
||||
if (eventHandler->type == SDL_KEYDOWN && eventHandler->key.repeat == 0)
|
||||
{
|
||||
switch (eventHandler->key.keysym.scancode)
|
||||
{
|
||||
#ifdef DEBUG
|
||||
case SDL_SCANCODE_G:
|
||||
debug->switchEnabled();
|
||||
options->cheat.invincible = debug->getEnabled();
|
||||
board.music = !debug->getEnabled();
|
||||
board.music ? JA_ResumeMusic() : JA_PauseMusic();
|
||||
break;
|
||||
|
||||
case SDL_SCANCODE_R:
|
||||
resource->reLoad();
|
||||
break;
|
||||
|
||||
case SDL_SCANCODE_W:
|
||||
goToRoom(BORDER_TOP);
|
||||
break;
|
||||
|
||||
case SDL_SCANCODE_A:
|
||||
goToRoom(BORDER_LEFT);
|
||||
break;
|
||||
|
||||
case SDL_SCANCODE_S:
|
||||
goToRoom(BORDER_BOTTOM);
|
||||
break;
|
||||
|
||||
case SDL_SCANCODE_D:
|
||||
goToRoom(BORDER_RIGHT);
|
||||
break;
|
||||
|
||||
case SDL_SCANCODE_F6:
|
||||
screen->showNotification("ACHIEVEMENT UNLOCKED!", "I LIKE MY MULTICOLOURED FRIENDS", 2);
|
||||
break;
|
||||
|
||||
case SDL_SCANCODE_F7:
|
||||
screen->showNotification("ACHIEVEMENT UNLOCKED!", "I LIKE MY MULTICOLOURED FRIENDS", 3);
|
||||
break;
|
||||
|
||||
case SDL_SCANCODE_F8:
|
||||
screen->showNotification("JAILDESIGNER IS LOGGED IN", "", 4);
|
||||
break;
|
||||
|
||||
case SDL_SCANCODE_F9:
|
||||
screen->showNotification("JAILDESIGNER IS LOGGED IN", "", 5);
|
||||
break;
|
||||
#endif
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Comprueba el teclado
|
||||
void Game::checkInput()
|
||||
{
|
||||
if (input->checkInput(input_exit, REPEAT_FALSE))
|
||||
{
|
||||
section->name = SECTION_TITLE;
|
||||
}
|
||||
|
||||
else if (input->checkInput(input_switch_music, REPEAT_FALSE))
|
||||
{
|
||||
board.music = !board.music;
|
||||
board.music ? JA_ResumeMusic() : JA_PauseMusic();
|
||||
}
|
||||
|
||||
else if (input->checkInput(input_pause, REPEAT_FALSE))
|
||||
{
|
||||
switchPause();
|
||||
}
|
||||
|
||||
else if (input->checkInput(input_toggle_border, REPEAT_FALSE))
|
||||
{
|
||||
screen->switchBorder();
|
||||
reLoadTextures();
|
||||
}
|
||||
|
||||
else if (input->checkInput(input_window_fullscreen, REPEAT_FALSE))
|
||||
{
|
||||
screen->switchVideoMode();
|
||||
reLoadTextures();
|
||||
}
|
||||
|
||||
else if (input->checkInput(input_window_dec_size, REPEAT_FALSE))
|
||||
{
|
||||
screen->decWindowSize();
|
||||
reLoadTextures();
|
||||
}
|
||||
|
||||
else if (input->checkInput(input_window_inc_size, REPEAT_FALSE))
|
||||
{
|
||||
screen->incWindowSize();
|
||||
reLoadTextures();
|
||||
}
|
||||
|
||||
else if (input->checkInput(input_swap_palette, REPEAT_FALSE))
|
||||
{
|
||||
switchPalette();
|
||||
}
|
||||
}
|
||||
|
||||
// Bucle para el juego
|
||||
void Game::run()
|
||||
{
|
||||
JA_PlayMusic(music);
|
||||
if (!board.music)
|
||||
{
|
||||
JA_PauseMusic();
|
||||
}
|
||||
|
||||
while (section->name == SECTION_GAME)
|
||||
{
|
||||
update();
|
||||
checkEvents();
|
||||
render();
|
||||
}
|
||||
|
||||
JA_StopMusic();
|
||||
}
|
||||
|
||||
// Actualiza el juego, las variables, comprueba la entrada, etc.
|
||||
void Game::update()
|
||||
{
|
||||
// Comprueba que la diferencia de ticks sea mayor a la velocidad del juego
|
||||
if (SDL_GetTicks() - ticks > ticksSpeed)
|
||||
{
|
||||
// Actualiza el contador de ticks
|
||||
ticks = SDL_GetTicks();
|
||||
|
||||
// Comprueba el teclado
|
||||
checkInput();
|
||||
|
||||
#ifdef DEBUG
|
||||
debug->clear();
|
||||
#endif
|
||||
|
||||
// Actualiza los objetos
|
||||
room->update();
|
||||
player->update();
|
||||
checkPlayerOnBorder();
|
||||
checkPlayerAndItems();
|
||||
checkPlayerAndEnemies();
|
||||
checkIfPlayerIsAlive();
|
||||
checkGameOver();
|
||||
checkEndGame();
|
||||
checkRestoringJail();
|
||||
checkSomeCheevos();
|
||||
scoreboard->update();
|
||||
input->update();
|
||||
|
||||
updateBlackScreen();
|
||||
|
||||
// Actualiza las notificaciones
|
||||
screen->updateNotifier();
|
||||
|
||||
#ifdef DEBUG
|
||||
updateDebugInfo();
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
// Pinta los objetos en pantalla
|
||||
void Game::render()
|
||||
{
|
||||
// Prepara para dibujar el frame
|
||||
screen->start();
|
||||
|
||||
// Dibuja los elementos del juego en orden
|
||||
room->renderMap();
|
||||
room->renderEnemies();
|
||||
room->renderItems();
|
||||
player->render();
|
||||
renderRoomName();
|
||||
scoreboard->render();
|
||||
renderBlackScreen();
|
||||
|
||||
#ifdef DEBUG
|
||||
// Debug info
|
||||
renderDebugInfo();
|
||||
#endif
|
||||
|
||||
// Actualiza la pantalla
|
||||
screen->blit();
|
||||
}
|
||||
|
||||
#ifdef DEBUG
|
||||
// Pasa la información de debug
|
||||
void Game::updateDebugInfo()
|
||||
{
|
||||
debug->add("X = " + std::to_string((int)player->x) + ", Y = " + std::to_string((int)player->y));
|
||||
debug->add("VX = " + std::to_string(player->vx).substr(0, 4) + ", VY = " + std::to_string(player->vy).substr(0, 4));
|
||||
debug->add("STATE = " + std::to_string(player->state));
|
||||
}
|
||||
|
||||
// Pone la información de debug en pantalla
|
||||
void Game::renderDebugInfo()
|
||||
{
|
||||
if (!debug->getEnabled())
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
// Borra el marcador
|
||||
SDL_Rect rect = {0, 18 * BLOCK, PLAY_AREA_WIDTH, GAMECANVAS_HEIGHT - PLAY_AREA_HEIGHT};
|
||||
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
|
||||
SDL_RenderFillRect(renderer, &rect);
|
||||
|
||||
// Pinta la rejilla
|
||||
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 32);
|
||||
for (int i = 0; i < PLAY_AREA_BOTTOM; i += 8)
|
||||
{ // Lineas horizontales
|
||||
SDL_RenderDrawLine(renderer, 0, i, PLAY_AREA_RIGHT, i);
|
||||
}
|
||||
for (int i = 0; i < PLAY_AREA_RIGHT; i += 8)
|
||||
{ // Lineas verticales
|
||||
SDL_RenderDrawLine(renderer, i, 0, i, PLAY_AREA_BOTTOM - 1);
|
||||
}
|
||||
|
||||
// Pinta el texto
|
||||
debug->setPos({1, 18 * 8});
|
||||
debug->render();
|
||||
}
|
||||
#endif
|
||||
|
||||
// Escribe el nombre de la pantalla
|
||||
void Game::renderRoomName()
|
||||
{
|
||||
// Dibuja la textura con el nombre de la habitación
|
||||
SDL_RenderCopy(renderer, roomNameTexture, nullptr, &roomNameRect);
|
||||
}
|
||||
|
||||
// Cambia de habitación
|
||||
bool Game::changeRoom(std::string file)
|
||||
{
|
||||
// En las habitaciones los limites tienen la cadena del fichero o un 0 en caso de no limitar con nada
|
||||
if (file == "0")
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
// Verifica que exista el fichero que se va a cargar
|
||||
if (asset->get(file) != "")
|
||||
{
|
||||
// Elimina la habitación actual
|
||||
delete room;
|
||||
room = nullptr;
|
||||
|
||||
// Crea un objeto habitación nuevo a partir del fichero
|
||||
room = new Room(resource->getRoom(file), renderer, screen, asset, options, itemTracker, &board.items, board.jailEnabled, debug);
|
||||
|
||||
// Pone el nombre de la habitación en la textura
|
||||
fillRoomNameTexture();
|
||||
|
||||
// Pone el color del marcador en función del color del borde de la habitación
|
||||
setScoreBoardColor();
|
||||
|
||||
if (roomTracker->addRoom(file))
|
||||
{
|
||||
// Incrementa el contador de habitaciones visitadas
|
||||
board.rooms++;
|
||||
options->stats.rooms = board.rooms;
|
||||
|
||||
// Actualiza las estadisticas
|
||||
stats->addVisit(room->getName());
|
||||
}
|
||||
|
||||
// Pasa la nueva habitación al jugador
|
||||
player->setRoom(room);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
// Comprueba si el jugador esta en el borde de la pantalla
|
||||
void Game::checkPlayerOnBorder()
|
||||
{
|
||||
if (player->getOnBorder())
|
||||
{
|
||||
const std::string roomName = room->getRoom(player->getBorder());
|
||||
if (changeRoom(roomName))
|
||||
{
|
||||
player->switchBorders();
|
||||
currentRoom = roomName;
|
||||
spawnPoint = player->getSpawnParams();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Comprueba las colisiones del jugador con los enemigos
|
||||
bool Game::checkPlayerAndEnemies()
|
||||
{
|
||||
const bool death = room->enemyCollision(player->getCollider());
|
||||
if (death)
|
||||
{
|
||||
killPlayer();
|
||||
}
|
||||
return death;
|
||||
}
|
||||
|
||||
// Comprueba las colisiones del jugador con los objetos
|
||||
void Game::checkPlayerAndItems()
|
||||
{
|
||||
room->itemCollision(player->getCollider());
|
||||
}
|
||||
|
||||
// Comprueba si el jugador esta vivo
|
||||
void Game::checkIfPlayerIsAlive()
|
||||
{
|
||||
if (!player->isAlive())
|
||||
{
|
||||
killPlayer();
|
||||
}
|
||||
}
|
||||
|
||||
// Comprueba si ha terminado la partida
|
||||
void Game::checkGameOver()
|
||||
{
|
||||
if (board.lives < 0 && blackScreenCounter > 17)
|
||||
{
|
||||
section->name = SECTION_GAME_OVER;
|
||||
}
|
||||
}
|
||||
|
||||
// Mata al jugador
|
||||
void Game::killPlayer()
|
||||
{
|
||||
if (options->cheat.invincible)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
// Resta una vida al jugador
|
||||
if (!options->cheat.infiniteLives)
|
||||
{
|
||||
board.lives--;
|
||||
}
|
||||
|
||||
// Actualiza las estadisticas
|
||||
stats->addDeath(room->getName());
|
||||
|
||||
// Invalida el logro de pasarse el juego sin morir
|
||||
cheevos->invalidate(11);
|
||||
|
||||
// Destruye la habitacion y el jugador
|
||||
delete room;
|
||||
delete this->player;
|
||||
|
||||
// Sonido
|
||||
JA_PlaySound(deathSound);
|
||||
|
||||
// Pone la pantalla en negro un tiempo
|
||||
setBlackScreen();
|
||||
|
||||
// Crea la nueva habitación y el nuevo jugador
|
||||
room = new Room(resource->getRoom(currentRoom), renderer, screen, asset, options, itemTracker, &board.items, board.jailEnabled, debug);
|
||||
const std::string playerPNG = options->cheat.altSkin ? "player2.png" : "player.png";
|
||||
const std::string playerANI = options->cheat.altSkin ? "player2.ani" : "player.ani";
|
||||
const player_t player = {spawnPoint, playerPNG, playerANI, renderer, resource, asset, options, input, room, debug};
|
||||
this->player = new Player(player);
|
||||
|
||||
// Pone los objetos en pausa mientras esta la habitación en negro
|
||||
room->pause();
|
||||
this->player->pause();
|
||||
}
|
||||
|
||||
// Recarga todas las texturas
|
||||
void Game::reLoadTextures()
|
||||
{
|
||||
if (options->console)
|
||||
{
|
||||
std::cout << "** RELOAD REQUESTED" << std::endl;
|
||||
}
|
||||
player->reLoadTexture();
|
||||
room->reLoadTexture();
|
||||
scoreboard->reLoadTexture();
|
||||
text->reLoadTexture();
|
||||
}
|
||||
|
||||
// Cambia la paleta
|
||||
void Game::switchPalette()
|
||||
{
|
||||
if (options->console)
|
||||
{
|
||||
std::cout << "** PALETTE SWITCH REQUESTED" << std::endl;
|
||||
}
|
||||
|
||||
// Modifica la variable
|
||||
options->palette = (options->palette == p_zxspectrum) ? p_zxarne : p_zxspectrum;
|
||||
|
||||
// Recarga las paletas
|
||||
room->reLoadPalette();
|
||||
player->reLoadPalette();
|
||||
scoreboard->reLoadPalette();
|
||||
|
||||
// Pone el color del marcador en función del color del borde de la habitación
|
||||
setScoreBoardColor();
|
||||
}
|
||||
|
||||
// Establece la pantalla en negro
|
||||
void Game::setBlackScreen()
|
||||
{
|
||||
blackScreen = true;
|
||||
}
|
||||
|
||||
// Actualiza las variables relativas a la pantalla en negro
|
||||
void Game::updateBlackScreen()
|
||||
{
|
||||
if (blackScreen)
|
||||
{
|
||||
blackScreenCounter++;
|
||||
if (blackScreenCounter > 20)
|
||||
{
|
||||
blackScreen = false;
|
||||
blackScreenCounter = 0;
|
||||
|
||||
player->resume();
|
||||
room->resume();
|
||||
screen->setBorderColor(room->getBorderColor());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Dibuja la pantalla negra
|
||||
void Game::renderBlackScreen()
|
||||
{
|
||||
if (blackScreen)
|
||||
{
|
||||
screen->clean();
|
||||
screen->setBorderColor(stringToColor(options->palette, "black"));
|
||||
}
|
||||
}
|
||||
|
||||
// Pone el color del marcador en función del color del borde de la habitación
|
||||
void Game::setScoreBoardColor()
|
||||
{
|
||||
// Obtiene el color del borde
|
||||
const color_t colorBorder = room->getBorderColor();
|
||||
|
||||
const bool isBlack = colorAreEqual(colorBorder, stringToColor(options->palette, "black"));
|
||||
const bool isBrightBlack = colorAreEqual(colorBorder, stringToColor(options->palette, "bright_black"));
|
||||
|
||||
// Si el color del borde es negro o negro brillante cambia el texto del marcador a blanco
|
||||
board.color = isBlack || isBrightBlack ? stringToColor(options->palette, "white") : colorBorder;
|
||||
}
|
||||
|
||||
// Comprueba si ha finalizado el juego
|
||||
bool Game::checkEndGame()
|
||||
{
|
||||
const bool isOnTheRoom = room->getName() == "THE JAIL"; // Estar en la habitación que toca
|
||||
const bool haveTheItems = board.items >= int(totalItems * 0.9f) || options->cheat.jailEnabled; // Con mas del 90% de los items recogidos
|
||||
const bool isOnTheDoor = player->getRect().x <= 128; // Y en la ubicación que toca (En la puerta)
|
||||
|
||||
if (haveTheItems)
|
||||
{
|
||||
board.jailEnabled = true;
|
||||
}
|
||||
|
||||
if (haveTheItems && isOnTheRoom && isOnTheDoor)
|
||||
{
|
||||
// Comprueba los logros de completar el juego
|
||||
checkEndGameCheevos();
|
||||
|
||||
section->name = SECTION_ENDING;
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
// Obtiene la cantidad total de items que hay en el mapeado del juego
|
||||
int Game::getTotalItems()
|
||||
{
|
||||
int items = 0;
|
||||
std::vector<res_room_t> *rooms = new std::vector<res_room_t>;
|
||||
rooms = resource->getAllRooms();
|
||||
|
||||
for (auto room : *rooms)
|
||||
{
|
||||
items += room.room->items.size();
|
||||
}
|
||||
|
||||
return items;
|
||||
}
|
||||
|
||||
// Va a la habitación designada
|
||||
void Game::goToRoom(int border)
|
||||
{
|
||||
const std::string roomName = room->getRoom(border);
|
||||
if (changeRoom(roomName))
|
||||
{
|
||||
currentRoom = roomName;
|
||||
}
|
||||
}
|
||||
|
||||
// Pone el juego en pausa
|
||||
void Game::switchPause()
|
||||
{
|
||||
if (paused)
|
||||
{
|
||||
player->resume();
|
||||
room->resume();
|
||||
scoreboard->resume();
|
||||
paused = false;
|
||||
}
|
||||
else
|
||||
{
|
||||
player->pause();
|
||||
room->pause();
|
||||
scoreboard->pause();
|
||||
paused = true;
|
||||
}
|
||||
}
|
||||
|
||||
// Da vidas al jugador cuando está en la Jail
|
||||
void Game::checkRestoringJail()
|
||||
{
|
||||
if (room->getName() != "THE JAIL" || board.lives == 9)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
static int counter = 0;
|
||||
|
||||
if (!paused)
|
||||
{
|
||||
counter++;
|
||||
}
|
||||
|
||||
// Incrementa el numero de vidas
|
||||
if (counter == 100)
|
||||
{
|
||||
counter = 0;
|
||||
board.lives++;
|
||||
JA_PlaySound(deathSound);
|
||||
|
||||
// Invalida el logro de completar el juego sin entrar a la jail
|
||||
const bool haveTheItems = board.items >= int(totalItems * 0.9f);
|
||||
if (!haveTheItems)
|
||||
{
|
||||
cheevos->invalidate(9);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Inicializa el diccionario de las estadísticas
|
||||
void Game::initStats()
|
||||
{
|
||||
std::vector<res_room_t> *rooms = new std::vector<res_room_t>;
|
||||
rooms = resource->getAllRooms();
|
||||
|
||||
for (auto room : *rooms)
|
||||
{
|
||||
stats->addDictionary(room.room->number, room.room->name);
|
||||
}
|
||||
|
||||
stats->init();
|
||||
}
|
||||
|
||||
// Crea la textura con el nombre de la habitación
|
||||
void Game::fillRoomNameTexture()
|
||||
{
|
||||
// Pone la textura como destino de renderizado
|
||||
SDL_SetRenderTarget(renderer, roomNameTexture);
|
||||
|
||||
// Rellena la textura de color
|
||||
const color_t color = stringToColor(options->palette, "white");
|
||||
SDL_SetRenderDrawColor(renderer, color.r, color.g, color.b, 0xFF);
|
||||
SDL_RenderClear(renderer);
|
||||
|
||||
// Escribe el texto en la textura
|
||||
text->writeDX(TXT_CENTER | TXT_COLOR, GAMECANVAS_CENTER_X, text->getCharacterSize() / 2, room->getName(), 1, room->getBGColor());
|
||||
|
||||
// Deja el renderizador por defecto
|
||||
SDL_SetRenderTarget(renderer, nullptr);
|
||||
}
|
||||
|
||||
// Comprueba algunos logros
|
||||
void Game::checkSomeCheevos()
|
||||
{
|
||||
// Logros sobre la cantidad de items
|
||||
if (board.items == totalItems)
|
||||
{
|
||||
cheevos->unlock(4);
|
||||
cheevos->unlock(3);
|
||||
cheevos->unlock(2);
|
||||
cheevos->unlock(1);
|
||||
}
|
||||
else if (board.items >= totalItems * 0.75f)
|
||||
{
|
||||
cheevos->unlock(3);
|
||||
cheevos->unlock(2);
|
||||
cheevos->unlock(1);
|
||||
}
|
||||
else if (board.items >= totalItems * 0.5f)
|
||||
{
|
||||
cheevos->unlock(2);
|
||||
cheevos->unlock(1);
|
||||
}
|
||||
else if (board.items >= totalItems * 0.25f)
|
||||
{
|
||||
cheevos->unlock(1);
|
||||
}
|
||||
|
||||
// Logros sobre las habitaciones visitadas
|
||||
if (board.rooms >= 60)
|
||||
{
|
||||
cheevos->unlock(7);
|
||||
cheevos->unlock(6);
|
||||
cheevos->unlock(5);
|
||||
}
|
||||
else if (board.rooms >= 40)
|
||||
{
|
||||
cheevos->unlock(6);
|
||||
cheevos->unlock(5);
|
||||
}
|
||||
else if (board.rooms >= 20)
|
||||
{
|
||||
cheevos->unlock(5);
|
||||
}
|
||||
}
|
||||
|
||||
// Comprueba los logros de completar el juego
|
||||
void Game::checkEndGameCheevos()
|
||||
{
|
||||
// "Complete the game"
|
||||
cheevos->unlock(8);
|
||||
|
||||
// "Complete the game without entering the jail"
|
||||
cheevos->unlock(9);
|
||||
|
||||
// "Complete the game with all items"
|
||||
if (board.items == totalItems)
|
||||
{
|
||||
cheevos->unlock(10);
|
||||
}
|
||||
|
||||
// "Complete the game without dying"
|
||||
cheevos->unlock(11);
|
||||
|
||||
// "Complete the game in under 30 minutes"
|
||||
if (scoreboard->getMinutes() < 30)
|
||||
{
|
||||
cheevos->unlock(12);
|
||||
}
|
||||
}
|
||||
@@ -1,165 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include <SDL2/SDL.h>
|
||||
#include "cheevos.h"
|
||||
#include "jail_engine/animatedsprite.h"
|
||||
#include "jail_engine/asset.h"
|
||||
#include "jail_engine/debug.h"
|
||||
#include "jail_engine/input.h"
|
||||
#include "jail_engine/jail_audio.h"
|
||||
#include "jail_engine/resource.h"
|
||||
#include "jail_engine/screen.h"
|
||||
#include "jail_engine/sprite.h"
|
||||
#include "jail_engine/text.h"
|
||||
#include "jail_engine/utils.h"
|
||||
#include "const.h"
|
||||
#include "item_tracker.h"
|
||||
#include "player.h"
|
||||
#include "room_tracker.h"
|
||||
#include "room.h"
|
||||
#include "scoreboard.h"
|
||||
#include "stats.h"
|
||||
#include "online.h"
|
||||
|
||||
#ifndef GAME_H
|
||||
#define GAME_H
|
||||
|
||||
class Game
|
||||
{
|
||||
private:
|
||||
// Objetos y punteros
|
||||
SDL_Renderer *renderer; // El renderizador de la ventana
|
||||
SDL_Event *eventHandler; // Manejador de eventos
|
||||
Screen *screen; // Objeto encargado de manejar el renderizador
|
||||
Room *room; // Objeto encargado de gestionar cada habitación del juego
|
||||
Player *player; // Objeto con el jugador
|
||||
ItemTracker *itemTracker; // Lleva el control de los objetos recogidos
|
||||
RoomTracker *roomTracker; // Lleva el control de las habitaciones visitadas
|
||||
Asset *asset; // Objeto con la ruta a todos los ficheros de recursos
|
||||
Input *input; // Objeto pata gestionar la entrada
|
||||
Text *text; // Objeto para los textos del juego
|
||||
Online *online; // Objeto para gestionar la lectura y escritura de datos en el servidor remoto
|
||||
ScoreBoard *scoreboard; // Objeto encargado de gestionar el marcador
|
||||
Cheevos *cheevos; // Objeto encargado de gestionar los logros del juego
|
||||
Resource *resource; // Objeto con los recursos
|
||||
Debug *debug; // Objeto para gestionar la información de debug
|
||||
options_t *options; // Puntero a las opciones del juego
|
||||
Stats *stats; // Objeto encargado de gestionar las estadísticas
|
||||
SDL_Texture *roomNameTexture; // Textura para escribir el nombre de la habitación
|
||||
section_t *section; // Seccion actual dentro del juego
|
||||
|
||||
// Variables
|
||||
JA_Music_t *music; // Musica que suena durante el juego
|
||||
Uint32 ticks; // Contador de ticks para ajustar la velocidad del programa
|
||||
Uint32 ticksSpeed; // Velocidad a la que se repiten los bucles del programa
|
||||
std::string currentRoom; // Fichero de la habitación actual
|
||||
playerSpawn_t spawnPoint; // Lugar de la habitación donde aparece el jugador
|
||||
JA_Sound_t *deathSound; // Sonido a reproducir cuando muere el jugador
|
||||
board_t board; // Estructura con los datos del marcador
|
||||
bool paused; // Indica si el juego se encuentra en pausa
|
||||
bool blackScreen; // Indica si la pantalla está en negro. Se utiliza para la muerte del jugador
|
||||
int blackScreenCounter; // Contador para temporizar la pantalla en negro
|
||||
int totalItems; // Cantidad total de items que hay en el mapeado del juego
|
||||
SDL_Rect roomNameRect; // Rectangulo donde pintar la textura con el nombre de la habitación
|
||||
|
||||
// Actualiza el juego, las variables, comprueba la entrada, etc.
|
||||
void update();
|
||||
|
||||
// Pinta los objetos en pantalla
|
||||
void render();
|
||||
|
||||
// Comprueba los eventos de la cola
|
||||
void checkEvents();
|
||||
|
||||
#ifdef DEBUG
|
||||
// Pone la información de debug en pantalla
|
||||
void updateDebugInfo();
|
||||
|
||||
// Pone la información de debug en pantalla
|
||||
void renderDebugInfo();
|
||||
#endif
|
||||
|
||||
// Escribe el nombre de la pantalla
|
||||
void renderRoomName();
|
||||
|
||||
// Cambia de habitación
|
||||
bool changeRoom(std::string file);
|
||||
|
||||
// Comprueba el teclado
|
||||
void checkInput();
|
||||
|
||||
// Comprueba si el jugador esta en el borde de la pantalla y actua
|
||||
void checkPlayerOnBorder();
|
||||
|
||||
// Comprueba las colisiones del jugador con los enemigos
|
||||
bool checkPlayerAndEnemies();
|
||||
|
||||
// Comprueba las colisiones del jugador con los objetos
|
||||
void checkPlayerAndItems();
|
||||
|
||||
// Comprueba si el jugador esta vivo
|
||||
void checkIfPlayerIsAlive();
|
||||
|
||||
// Comprueba si ha terminado la partida
|
||||
void checkGameOver();
|
||||
|
||||
// Mata al jugador
|
||||
void killPlayer();
|
||||
|
||||
// Recarga todas las texturas
|
||||
void reLoadTextures();
|
||||
|
||||
// Cambia la paleta
|
||||
void switchPalette();
|
||||
|
||||
// Establece la pantalla en negro
|
||||
void setBlackScreen();
|
||||
|
||||
// Actualiza las variables relativas a la pantalla en negro
|
||||
void updateBlackScreen();
|
||||
|
||||
// Dibuja la pantalla negra
|
||||
void renderBlackScreen();
|
||||
|
||||
// Pone el color del marcador en función del color del borde de la habitación
|
||||
void setScoreBoardColor();
|
||||
|
||||
// Comprueba si ha finalizado el juego
|
||||
bool checkEndGame();
|
||||
|
||||
// Obtiene la cantidad total de items que hay en el mapeado del juego
|
||||
int getTotalItems();
|
||||
|
||||
// Va a la habitación designada
|
||||
void goToRoom(int border);
|
||||
|
||||
// Pone el juego en pausa
|
||||
void switchPause();
|
||||
|
||||
// Da vidas al jugador cuando está en la Jail
|
||||
void checkRestoringJail();
|
||||
|
||||
// Inicializa el diccionario de las estadísticas
|
||||
void initStats();
|
||||
|
||||
// Pone el nombre de la habitación en la textura
|
||||
void fillRoomNameTexture();
|
||||
|
||||
// Comprueba algunos logros
|
||||
void checkSomeCheevos();
|
||||
|
||||
// Comprueba los logros de completar el juego
|
||||
void checkEndGameCheevos();
|
||||
|
||||
public:
|
||||
// Constructor
|
||||
Game(SDL_Renderer *renderer, Screen *screen, Resource *resource, Asset *asset, Online *online, options_t *options, Input *input, section_t *section, Debug *debug);
|
||||
|
||||
// Destructor
|
||||
~Game();
|
||||
|
||||
// Bucle para el juego
|
||||
void run();
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -1,358 +0,0 @@
|
||||
#include "gamestate_loading_screen.h"
|
||||
|
||||
// Constructor
|
||||
LoadingScreen::LoadingScreen(SDL_Renderer *renderer, Screen *screen, Resource *resource, Asset *asset, Input *input, options_t *options, section_t *section)
|
||||
{
|
||||
// Copia la dirección de los objetos
|
||||
this->resource = resource;
|
||||
this->renderer = renderer;
|
||||
this->screen = screen;
|
||||
this->asset = asset;
|
||||
this->input = input;
|
||||
this->options = options;
|
||||
this->section = section;
|
||||
|
||||
// Reserva memoria para los punteros
|
||||
eventHandler = new SDL_Event();
|
||||
if (options->palette == p_zxspectrum)
|
||||
{
|
||||
loadingScreenTexture1 = resource->getTexture("loading_screen_bn.png");
|
||||
loadingScreenTexture2 = resource->getTexture("loading_screen_color.png");
|
||||
}
|
||||
else if (options->palette == p_zxarne)
|
||||
{
|
||||
loadingScreenTexture1 = resource->getTexture("loading_screen_bn_zxarne.png");
|
||||
loadingScreenTexture2 = resource->getTexture("loading_screen_color_zxarne.png");
|
||||
}
|
||||
sprite1 = new Sprite(0, 0, loadingScreenTexture1->getWidth(), loadingScreenTexture1->getHeight(), loadingScreenTexture1, renderer);
|
||||
sprite2 = new Sprite(0, 0, loadingScreenTexture2->getWidth(), loadingScreenTexture2->getHeight(), loadingScreenTexture2, renderer);
|
||||
loadingSound1 = JA_LoadMusic(asset->get("loading_sound1.ogg").c_str());
|
||||
loadingSound2 = JA_LoadMusic(asset->get("loading_sound2.ogg").c_str());
|
||||
loadingSound3 = JA_LoadMusic(asset->get("loading_sound3.ogg").c_str());
|
||||
|
||||
// Inicializa variables
|
||||
preCounter = 0;
|
||||
counter = 0;
|
||||
section->name = SECTION_LOADING_SCREEN;
|
||||
section->subsection = 0;
|
||||
ticks = 0;
|
||||
ticksSpeed = 15;
|
||||
loadCounter = 0;
|
||||
loadingFirstPart = true;
|
||||
loadRect = {0, 0, 51, 1};
|
||||
|
||||
// Establece el orden de las lineas para imitar el direccionamiento de memoria del spectrum
|
||||
for (int i = 0; i < 192; ++i)
|
||||
{
|
||||
if (i < 64)
|
||||
{ // Primer bloque de 2K
|
||||
lineIndex[i] = ((i % 8) * 8) + (i / 8);
|
||||
}
|
||||
|
||||
else if (i >= 64 && i < 128)
|
||||
{ // Segundo bloque de 2K
|
||||
lineIndex[i] = 64 + ((i % 8) * 8) + ((i - 64) / 8);
|
||||
}
|
||||
|
||||
else if (i >= 128 && i < 192)
|
||||
{ // tercer bloque de 2K
|
||||
lineIndex[i] = 128 + ((i % 8) * 8) + ((i - 128) / 8);
|
||||
}
|
||||
}
|
||||
|
||||
// Cambia el color del borde
|
||||
screen->setBorderColor(stringToColor(options->palette, "black"));
|
||||
}
|
||||
|
||||
// Destructor
|
||||
LoadingScreen::~LoadingScreen()
|
||||
{
|
||||
delete sprite1;
|
||||
delete sprite2;
|
||||
delete eventHandler;
|
||||
JA_DeleteMusic(loadingSound1);
|
||||
JA_DeleteMusic(loadingSound2);
|
||||
JA_DeleteMusic(loadingSound3);
|
||||
}
|
||||
|
||||
// Comprueba el manejador de eventos
|
||||
void LoadingScreen::checkEvents()
|
||||
{
|
||||
// Comprueba los eventos que hay en la cola
|
||||
while (SDL_PollEvent(eventHandler) != 0)
|
||||
{
|
||||
// Evento de salida de la aplicación
|
||||
if (eventHandler->type == SDL_QUIT)
|
||||
{
|
||||
section->name = SECTION_QUIT;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Comprueba las entradas
|
||||
void LoadingScreen::checkInput()
|
||||
{
|
||||
|
||||
if (input->checkInput(input_exit, REPEAT_FALSE))
|
||||
{
|
||||
section->name = SECTION_QUIT;
|
||||
}
|
||||
|
||||
else if (input->checkInput(input_toggle_border, REPEAT_FALSE))
|
||||
{
|
||||
screen->switchBorder();
|
||||
}
|
||||
|
||||
else if (input->checkInput(input_window_fullscreen, REPEAT_FALSE))
|
||||
{
|
||||
screen->switchVideoMode();
|
||||
}
|
||||
|
||||
else if (input->checkInput(input_window_dec_size, REPEAT_FALSE))
|
||||
{
|
||||
screen->decWindowSize();
|
||||
}
|
||||
|
||||
else if (input->checkInput(input_window_inc_size, REPEAT_FALSE))
|
||||
{
|
||||
screen->incWindowSize();
|
||||
}
|
||||
|
||||
else if (input->checkInput(input_swap_palette, REPEAT_FALSE))
|
||||
{
|
||||
switchPalette();
|
||||
}
|
||||
|
||||
else if (input->checkInput(input_pause, REPEAT_FALSE) || input->checkInput(input_accept, REPEAT_FALSE) || input->checkInput(input_jump, REPEAT_FALSE))
|
||||
{
|
||||
section->name = SECTION_TITLE;
|
||||
section->subsection = 0;
|
||||
}
|
||||
}
|
||||
|
||||
// Gestiona el contador de carga
|
||||
void LoadingScreen::updateLoad()
|
||||
{
|
||||
// Primera parte de la carga, la parte en blanco y negro
|
||||
if (loadingFirstPart)
|
||||
{
|
||||
// Cada 5 pasos el loadCounter se incrementa en uno
|
||||
const int numSteps = 5;
|
||||
const int step = 51;
|
||||
loadCounter = counter / numSteps;
|
||||
|
||||
if (loadCounter < 192)
|
||||
{
|
||||
loadRect.x = step * (counter % numSteps);
|
||||
loadRect.y = lineIndex[loadCounter];
|
||||
sprite1->setSpriteClip(loadRect);
|
||||
sprite1->setRect(loadRect);
|
||||
}
|
||||
// Una vez actualizadas las 192 lineas, pasa a la segunda fase de la carga
|
||||
else if (loadCounter == 192)
|
||||
{
|
||||
loadingFirstPart = false;
|
||||
loadCounter = 0;
|
||||
loadRect = {0, 0, 16, 8};
|
||||
sprite2->setRect(loadRect);
|
||||
sprite2->setSpriteClip(loadRect);
|
||||
JA_PlayMusic(loadingSound3);
|
||||
}
|
||||
}
|
||||
// Segunda parte de la carga, la parte de los bloques en color
|
||||
else
|
||||
{
|
||||
loadCounter += 2;
|
||||
loadRect.x = (loadCounter * 8) % 256;
|
||||
loadRect.y = (loadCounter / 32) * 8;
|
||||
sprite2->setSpriteClip(loadRect);
|
||||
sprite2->setRect(loadRect);
|
||||
|
||||
// Comprueba si ha terminado la intro
|
||||
if (loadCounter >= 768)
|
||||
{
|
||||
section->name = SECTION_TITLE;
|
||||
section->subsection = SUBSECTION_TITLE_WITH_LOADING_SCREEN;
|
||||
JA_StopMusic();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Gestiona el contador interno
|
||||
void LoadingScreen::updateCounter()
|
||||
{
|
||||
(preCounter >= 50) ? counter++ : preCounter++;
|
||||
|
||||
if (counter == 1)
|
||||
{
|
||||
JA_PlayMusic(loadingSound2);
|
||||
}
|
||||
}
|
||||
|
||||
// Dibuja la pantalla de carga
|
||||
void LoadingScreen::renderLoad()
|
||||
{
|
||||
loadingFirstPart ? sprite1->render() : sprite2->render();
|
||||
}
|
||||
|
||||
// Dibuja el efecto de carga en el borde
|
||||
void LoadingScreen::renderBorder()
|
||||
{
|
||||
// Pinta el borde de colro azul
|
||||
color_t color = stringToColor(options->palette, "blue");
|
||||
SDL_SetRenderDrawColor(renderer, color.r, color.g, color.b, 0xFF);
|
||||
SDL_RenderClear(renderer);
|
||||
|
||||
// Añade lineas amarillas
|
||||
color = stringToColor(options->palette, "yellow");
|
||||
SDL_SetRenderDrawColor(renderer, color.r, color.g, color.b, 0xFF);
|
||||
const int width = GAMECANVAS_WIDTH + (options->borderWidth * 2);
|
||||
const int height = GAMECANVAS_HEIGHT + (options->borderHeight * 2);
|
||||
bool drawEnabled = rand() % 2 == 0 ? true : false;
|
||||
// for (int i = 0; i < height; ++i)
|
||||
//{
|
||||
// if (rand() % 2 == 0)
|
||||
// {
|
||||
// SDL_RenderDrawLine(renderer, 0, i, width, i);
|
||||
// }
|
||||
// }
|
||||
int row = 0;
|
||||
int rowSize = 1;
|
||||
while (row < height)
|
||||
{
|
||||
rowSize = (rand() % 4) + 3;
|
||||
if (drawEnabled)
|
||||
for (int i = row; i < row + rowSize; ++i)
|
||||
{
|
||||
SDL_RenderDrawLine(renderer, 0, i, width, i);
|
||||
}
|
||||
row += rowSize;
|
||||
drawEnabled = !drawEnabled;
|
||||
}
|
||||
}
|
||||
|
||||
// Actualiza las variables
|
||||
void LoadingScreen::update()
|
||||
{
|
||||
// Comprueba que la diferencia de ticks sea mayor a la velocidad del juego
|
||||
if (SDL_GetTicks() - ticks > ticksSpeed)
|
||||
{
|
||||
// Actualiza el contador de ticks
|
||||
ticks = SDL_GetTicks();
|
||||
|
||||
// Comprueba las entradas
|
||||
checkInput();
|
||||
|
||||
// Gestiona el contador interno
|
||||
updateCounter();
|
||||
|
||||
// Gestiona el contador de carga
|
||||
updateLoad();
|
||||
|
||||
// Actualiza las notificaciones
|
||||
screen->updateNotifier();
|
||||
}
|
||||
}
|
||||
|
||||
// Dibuja en pantalla
|
||||
void LoadingScreen::render()
|
||||
{
|
||||
if (options->borderEnabled)
|
||||
{
|
||||
// Prepara para empezar a dibujar en la textura del borde
|
||||
screen->startDrawOnBorder();
|
||||
|
||||
// Dibuja el efecto de carga en el borde
|
||||
renderBorder();
|
||||
}
|
||||
|
||||
// Prepara para empezar a dibujar en la textura de juego
|
||||
screen->start();
|
||||
|
||||
// Dibuja la pantalla de carga
|
||||
renderLoad();
|
||||
|
||||
// Vuelca el contenido del renderizador en pantalla
|
||||
screen->blit();
|
||||
}
|
||||
|
||||
// Bucle para el logo del juego
|
||||
void LoadingScreen::run()
|
||||
{
|
||||
// Inicia el sonido de carga
|
||||
JA_SetVolume(64);
|
||||
JA_PlayMusic(loadingSound1);
|
||||
|
||||
// Limpia la pantalla
|
||||
screen->start();
|
||||
screen->clean();
|
||||
screen->blit();
|
||||
|
||||
while (section->name == SECTION_LOADING_SCREEN)
|
||||
{
|
||||
update();
|
||||
checkEvents();
|
||||
render();
|
||||
}
|
||||
|
||||
JA_SetVolume(128);
|
||||
}
|
||||
|
||||
// Cambia la paleta
|
||||
void LoadingScreen::switchPalette()
|
||||
{
|
||||
if (options->palette == p_zxspectrum)
|
||||
{
|
||||
options->palette = p_zxarne;
|
||||
sprite1->setTexture(resource->getTexture("loading_screen_bn_zxarne.png"));
|
||||
sprite2->setTexture(resource->getTexture("loading_screen_color_zxarne.png"));
|
||||
}
|
||||
else
|
||||
{
|
||||
options->palette = p_zxspectrum;
|
||||
sprite1->setTexture(resource->getTexture("loading_screen_bn.png"));
|
||||
sprite2->setTexture(resource->getTexture("loading_screen_color.png"));
|
||||
}
|
||||
|
||||
recreateLoadingScreen();
|
||||
}
|
||||
|
||||
// Reconstruye la pantalla de carga
|
||||
void LoadingScreen::recreateLoadingScreen()
|
||||
{
|
||||
// Prepara para empezar a dibujar en la textura de juego
|
||||
screen->start();
|
||||
|
||||
// Primera parte de la carga, la parte en blanco y negro
|
||||
if (loadingFirstPart)
|
||||
{
|
||||
const int numSteps = 5;
|
||||
const int step = 51;
|
||||
|
||||
for (int i = 0; i <= counter; i++)
|
||||
{
|
||||
loadCounter = i / numSteps;
|
||||
loadRect.x = step * (i % numSteps);
|
||||
loadRect.y = lineIndex[loadCounter];
|
||||
sprite1->setSpriteClip(loadRect);
|
||||
sprite1->setRect(loadRect);
|
||||
sprite1->render();
|
||||
}
|
||||
}
|
||||
// Segunda parte de la carga, la parte de los bloques en color
|
||||
else
|
||||
{
|
||||
for (int i = 0; i <= loadCounter; i++)
|
||||
{
|
||||
loadRect.x = (i * 8) % 256;
|
||||
loadRect.y = (i / 32) * 8;
|
||||
sprite2->setSpriteClip(loadRect);
|
||||
sprite2->setRect(loadRect);
|
||||
sprite2->render();
|
||||
}
|
||||
}
|
||||
|
||||
// Vuelca el contenido del renderizador en pantalla
|
||||
screen->blit();
|
||||
}
|
||||
@@ -1,91 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include <SDL2/SDL.h>
|
||||
#include "jail_engine/asset.h"
|
||||
#include "jail_engine/input.h"
|
||||
#include "jail_engine/jail_audio.h"
|
||||
#include "jail_engine/resource.h"
|
||||
#include "jail_engine/screen.h"
|
||||
#include "jail_engine/sprite.h"
|
||||
#include "jail_engine/utils.h"
|
||||
#include "const.h"
|
||||
#include <vector>
|
||||
|
||||
#include <string>
|
||||
#include "jail_engine/text.h"
|
||||
|
||||
#ifndef LOADING_SCREEN_H
|
||||
#define LOADING_SCREEN_H
|
||||
|
||||
class LoadingScreen
|
||||
{
|
||||
private:
|
||||
// Objetos y punteros
|
||||
SDL_Renderer *renderer; // El renderizador de la ventana
|
||||
Screen *screen; // Objeto encargado de dibujar en pantalla
|
||||
Resource *resource; // Objeto con los recursos
|
||||
Asset *asset; // Objeto con los ficheros de recursos
|
||||
Input *input; // Objeto pata gestionar la entrada
|
||||
Texture *loadingScreenTexture1; // Textura con la pantalla de carga en blanco y negro
|
||||
Texture *loadingScreenTexture2; // Textura con la pantalla de carga en color
|
||||
SDL_Event *eventHandler; // Manejador de eventos
|
||||
Sprite *sprite1; // Sprite para manejar la textura loadingScreenTexture1
|
||||
Sprite *sprite2; // Sprite para manejar la textura loadingScreenTexture2
|
||||
options_t *options; // Puntero a las opciones del juego
|
||||
section_t *section; // Estado del bucle principal para saber si continua o se sale
|
||||
|
||||
// Variables
|
||||
int preCounter; // Contador previo para realizar una pausa inicial
|
||||
int counter; // Contador
|
||||
Uint32 ticks; // Contador de ticks para ajustar la velocidad del programa
|
||||
Uint32 ticksSpeed; // Velocidad a la que se repiten los bucles del programa
|
||||
int loadCounter; // Contador para controlar las cargas
|
||||
bool loadingFirstPart; // Para saber en que parte de la carga se encuentra
|
||||
JA_Music_t *loadingSound1; // Sonidos para imitar la carga tipo spectrum
|
||||
JA_Music_t *loadingSound2; // Sonidos para imitar la carga tipo spectrum
|
||||
JA_Music_t *loadingSound3; // Sonidos para imitar la carga tipo spectrum
|
||||
int lineIndex[192]; // El orden en el que se procesan las 192 lineas de la pantalla de carga
|
||||
SDL_Rect loadRect; // Rectangulo para dibujar la pantalla de carga
|
||||
|
||||
// Actualiza las variables
|
||||
void update();
|
||||
|
||||
// Dibuja en pantalla
|
||||
void render();
|
||||
|
||||
// Comprueba el manejador de eventos
|
||||
void checkEvents();
|
||||
|
||||
// Comprueba las entradas
|
||||
void checkInput();
|
||||
|
||||
// Gestiona el contador interno
|
||||
void updateCounter();
|
||||
|
||||
// Gestiona el contador de carga
|
||||
void updateLoad();
|
||||
|
||||
// Dibuja la pantalla de carga
|
||||
void renderLoad();
|
||||
|
||||
// Dibuja el efecto de carga en el borde
|
||||
void renderBorder();
|
||||
|
||||
// Cambia la paleta
|
||||
void switchPalette();
|
||||
|
||||
// Reconstruye la pantalla de carga
|
||||
void recreateLoadingScreen();
|
||||
|
||||
public:
|
||||
// Constructor
|
||||
LoadingScreen(SDL_Renderer *renderer, Screen *screen, Resource *resource, Asset *asset, Input *input, options_t *options, section_t *section);
|
||||
|
||||
// Destructor
|
||||
~LoadingScreen();
|
||||
|
||||
// Bucle principal
|
||||
void run();
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -1,332 +0,0 @@
|
||||
#include "gamestate_logo.h"
|
||||
#include <iostream>
|
||||
|
||||
// Constructor
|
||||
Logo::Logo(SDL_Renderer *renderer, Screen *screen, Resource *resource, Asset *asset, Input *input, options_t *options, section_t *section)
|
||||
{
|
||||
// Copia la dirección de los objetos
|
||||
this->resource = resource;
|
||||
this->renderer = renderer;
|
||||
this->screen = screen;
|
||||
this->asset = asset;
|
||||
this->input = input;
|
||||
this->options = options;
|
||||
this->section = section;
|
||||
|
||||
// Reserva memoria para los punteros
|
||||
eventHandler = new SDL_Event();
|
||||
texture = resource->getTexture("jailgames.png");
|
||||
texture2 = resource->getTexture("since_1998.png");
|
||||
sprite2 = new Sprite((256 - texture2->getWidth()) / 2, 83 + texture->getHeight() + 5, texture2->getWidth(), texture2->getHeight(), texture2, renderer);
|
||||
sprite2->setSpriteClip(0, 0, texture2->getWidth(), texture2->getHeight());
|
||||
texture2->setColor(0, 0, 0);
|
||||
|
||||
// Crea los sprites de cada linea
|
||||
for (int i = 0; i < texture->getHeight(); ++i)
|
||||
{
|
||||
sprite.push_back(new Sprite(0, i, texture->getWidth(), 1, texture, renderer));
|
||||
sprite.back()->setSpriteClip(0, i, texture->getWidth(), 1);
|
||||
if (i % 2 == 0)
|
||||
{
|
||||
sprite[i]->setPosX(256 + (i * 3));
|
||||
}
|
||||
else
|
||||
{
|
||||
sprite[i]->setPosX(-181 - (i * 3));
|
||||
}
|
||||
sprite[i]->setPosY(83 + i);
|
||||
}
|
||||
|
||||
// Inicializa variables
|
||||
counter = 0;
|
||||
section->name = SECTION_LOGO;
|
||||
ticks = 0;
|
||||
ticksSpeed = 15;
|
||||
initFade = 300;
|
||||
endLogo = 400;
|
||||
postLogo = 20;
|
||||
|
||||
// Inicializa el vector de colores
|
||||
const std::vector<std::string> vColors = {"black", "blue", "red", "magenta", "green", "cyan", "yellow", "bright_white"};
|
||||
for (auto v : vColors)
|
||||
{
|
||||
color.push_back(stringToColor(options->palette, v));
|
||||
}
|
||||
|
||||
// Cambia el color del borde
|
||||
screen->setBorderColor(stringToColor(options->palette, "black"));
|
||||
}
|
||||
|
||||
// Destructor
|
||||
Logo::~Logo()
|
||||
{
|
||||
for (auto s : sprite)
|
||||
{
|
||||
delete s;
|
||||
}
|
||||
|
||||
delete sprite2;
|
||||
delete eventHandler;
|
||||
}
|
||||
|
||||
// Comprueba el manejador de eventos
|
||||
void Logo::checkEvents()
|
||||
{
|
||||
// Comprueba los eventos que hay en la cola
|
||||
while (SDL_PollEvent(eventHandler) != 0)
|
||||
{
|
||||
// Evento de salida de la aplicación
|
||||
if (eventHandler->type == SDL_QUIT)
|
||||
{
|
||||
section->name = SECTION_QUIT;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Comprueba las entradas
|
||||
void Logo::checkInput()
|
||||
{
|
||||
if (input->checkInput(input_exit, REPEAT_FALSE))
|
||||
{
|
||||
section->name = SECTION_TITLE;
|
||||
}
|
||||
|
||||
else if (input->checkInput(input_toggle_border, REPEAT_FALSE))
|
||||
{
|
||||
screen->switchBorder();
|
||||
}
|
||||
|
||||
else if (input->checkInput(input_window_fullscreen, REPEAT_FALSE))
|
||||
{
|
||||
screen->switchVideoMode();
|
||||
}
|
||||
|
||||
else if (input->checkInput(input_window_dec_size, REPEAT_FALSE))
|
||||
{
|
||||
screen->decWindowSize();
|
||||
}
|
||||
|
||||
else if (input->checkInput(input_window_inc_size, REPEAT_FALSE))
|
||||
{
|
||||
screen->incWindowSize();
|
||||
}
|
||||
|
||||
else if (input->checkInput(input_swap_palette, REPEAT_FALSE))
|
||||
{
|
||||
switchPalette();
|
||||
}
|
||||
|
||||
else if (input->checkInput(input_pause, REPEAT_FALSE) || input->checkInput(input_accept, REPEAT_FALSE) || input->checkInput(input_jump, REPEAT_FALSE))
|
||||
{
|
||||
section->subsection = SUBSECTION_LOGO_TO_TITLE;
|
||||
endSection();
|
||||
}
|
||||
}
|
||||
|
||||
// Gestiona el logo de JAILGAME
|
||||
void Logo::updateJAILGAMES()
|
||||
{
|
||||
if (counter > 30)
|
||||
{
|
||||
for (int i = 1; i < (int)sprite.size(); ++i)
|
||||
{
|
||||
const int speed = 8;
|
||||
const int dest = 37;
|
||||
if (sprite[i]->getPosX() != 37)
|
||||
{
|
||||
if (i % 2 == 0)
|
||||
{
|
||||
sprite[i]->incPosX(-speed);
|
||||
if (sprite[i]->getPosX() < dest)
|
||||
{
|
||||
sprite[i]->setPosX(dest);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
sprite[i]->incPosX(speed);
|
||||
if (sprite[i]->getPosX() > dest)
|
||||
{
|
||||
sprite[i]->setPosX(dest);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Gestiona el color de las texturas
|
||||
void Logo::updateTextureColors()
|
||||
{
|
||||
const int ini = 70;
|
||||
const int inc = 4;
|
||||
|
||||
if (counter == ini + inc * 0)
|
||||
{
|
||||
texture2->setColor(color[0].r, color[0].g, color[0].b);
|
||||
}
|
||||
|
||||
else if (counter == ini + inc * 1)
|
||||
{
|
||||
texture2->setColor(color[1].r, color[1].g, color[1].b);
|
||||
}
|
||||
|
||||
else if (counter == ini + inc * 2)
|
||||
{
|
||||
texture2->setColor(color[2].r, color[2].g, color[2].b);
|
||||
}
|
||||
|
||||
else if (counter == ini + inc * 3)
|
||||
{
|
||||
texture2->setColor(color[3].r, color[3].g, color[3].b);
|
||||
}
|
||||
|
||||
else if (counter == ini + inc * 4)
|
||||
{
|
||||
texture2->setColor(color[4].r, color[4].g, color[4].b);
|
||||
}
|
||||
|
||||
else if (counter == ini + inc * 5)
|
||||
{
|
||||
texture2->setColor(color[5].r, color[5].g, color[5].b);
|
||||
}
|
||||
|
||||
else if (counter == ini + inc * 6)
|
||||
{
|
||||
texture2->setColor(color[6].r, color[6].g, color[6].b);
|
||||
}
|
||||
|
||||
else if (counter == ini + inc * 7)
|
||||
{
|
||||
texture2->setColor(color[7].r, color[7].g, color[7].b);
|
||||
}
|
||||
|
||||
else if (counter == initFade + inc * 0)
|
||||
{
|
||||
texture->setColor(color[6].r, color[6].g, color[6].b);
|
||||
texture2->setColor(color[6].r, color[6].g, color[6].b);
|
||||
}
|
||||
|
||||
else if (counter == initFade + inc * 1)
|
||||
{
|
||||
texture->setColor(color[5].r, color[5].g, color[5].b);
|
||||
texture2->setColor(color[5].r, color[5].g, color[5].b);
|
||||
}
|
||||
|
||||
else if (counter == initFade + inc * 2)
|
||||
{
|
||||
texture->setColor(color[4].r, color[4].g, color[4].b);
|
||||
texture2->setColor(color[4].r, color[4].g, color[4].b);
|
||||
}
|
||||
|
||||
else if (counter == initFade + inc * 3)
|
||||
{
|
||||
texture->setColor(color[3].r, color[3].g, color[3].b);
|
||||
texture2->setColor(color[3].r, color[3].g, color[3].b);
|
||||
}
|
||||
|
||||
else if (counter == initFade + inc * 4)
|
||||
{
|
||||
texture->setColor(color[2].r, color[2].g, color[2].b);
|
||||
texture2->setColor(color[2].r, color[2].g, color[2].b);
|
||||
}
|
||||
|
||||
else if (counter == initFade + inc * 5)
|
||||
{
|
||||
texture->setColor(color[1].r, color[1].g, color[1].b);
|
||||
texture2->setColor(color[1].r, color[1].g, color[1].b);
|
||||
}
|
||||
|
||||
else if (counter == initFade + inc * 6)
|
||||
{
|
||||
texture->setColor(color[0].r, color[0].g, color[0].b);
|
||||
texture2->setColor(color[0].r, color[0].g, color[0].b);
|
||||
}
|
||||
}
|
||||
|
||||
// Actualiza las variables
|
||||
void Logo::update()
|
||||
{
|
||||
// Comprueba que la diferencia de ticks sea mayor a la velocidad del juego
|
||||
if (SDL_GetTicks() - ticks > ticksSpeed)
|
||||
{
|
||||
// Actualiza el contador de ticks
|
||||
ticks = SDL_GetTicks();
|
||||
|
||||
// Comprueba las entradas
|
||||
checkInput();
|
||||
|
||||
// Incrementa el contador
|
||||
counter++;
|
||||
|
||||
// Gestiona el logo de JAILGAME
|
||||
updateJAILGAMES();
|
||||
|
||||
// Gestiona el color de las texturas
|
||||
updateTextureColors();
|
||||
|
||||
// Actualiza las notificaciones
|
||||
screen->updateNotifier();
|
||||
|
||||
// Comprueba si ha terminado el logo
|
||||
if (counter == endLogo + postLogo)
|
||||
{
|
||||
endSection();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Dibuja en pantalla
|
||||
void Logo::render()
|
||||
{
|
||||
// Prepara para empezar a dibujar en la textura de juego
|
||||
screen->start();
|
||||
|
||||
// Limpia la pantalla
|
||||
screen->clean();
|
||||
|
||||
// Dibuja los objetos
|
||||
for (auto s : sprite)
|
||||
{
|
||||
s->render();
|
||||
}
|
||||
sprite2->render();
|
||||
|
||||
// Vuelca el contenido del renderizador en pantalla
|
||||
screen->blit();
|
||||
}
|
||||
|
||||
// Bucle para el logo del juego
|
||||
void Logo::run()
|
||||
{
|
||||
// Detiene la música
|
||||
JA_StopMusic();
|
||||
|
||||
while (section->name == SECTION_LOGO)
|
||||
{
|
||||
update();
|
||||
checkEvents();
|
||||
render();
|
||||
}
|
||||
}
|
||||
|
||||
// Cambia la paleta
|
||||
void Logo::switchPalette()
|
||||
{
|
||||
options->palette = options->palette == p_zxspectrum ? p_zxarne : p_zxspectrum;
|
||||
}
|
||||
|
||||
// Termina la sección
|
||||
void Logo::endSection()
|
||||
{
|
||||
if (section->subsection == SUBSECTION_LOGO_TO_TITLE)
|
||||
{
|
||||
section->name = SECTION_TITLE;
|
||||
}
|
||||
|
||||
else if (section->subsection == SUBSECTION_LOGO_TO_INTRO)
|
||||
{
|
||||
section->name = SECTION_LOADING_SCREEN;
|
||||
}
|
||||
}
|
||||
@@ -1,78 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include <SDL2/SDL.h>
|
||||
#include "jail_engine/asset.h"
|
||||
#include "jail_engine/input.h"
|
||||
#include "jail_engine/jail_audio.h"
|
||||
#include "jail_engine/resource.h"
|
||||
#include "jail_engine/screen.h"
|
||||
#include "jail_engine/sprite.h"
|
||||
#include "jail_engine/utils.h"
|
||||
#include "const.h"
|
||||
#include <vector>
|
||||
|
||||
#ifndef LOGO_H
|
||||
#define LOGO_H
|
||||
|
||||
class Logo
|
||||
{
|
||||
private:
|
||||
// Objetos y punteros
|
||||
SDL_Renderer *renderer; // El renderizador de la ventana
|
||||
Screen *screen; // Objeto encargado de dibujar en pantalla
|
||||
Resource *resource; // Objeto con los recursos
|
||||
Asset *asset; // Objeto con los ficheros de recursos
|
||||
Input *input; // Objeto pata gestionar la entrada
|
||||
Texture *texture; // Textura con los graficos "JAILGAMES"
|
||||
Texture *texture2; // Textura con los graficos "Since 1998"
|
||||
SDL_Event *eventHandler; // Manejador de eventos
|
||||
std::vector<Sprite *> sprite; // Vector con los sprites de cada linea que forman el bitmap JAILGAMES
|
||||
Sprite *sprite2; // Sprite para manejar la textura2
|
||||
options_t *options; // Puntero a las opciones del juego
|
||||
section_t *section; // Estado del bucle principal para saber si continua o se sale
|
||||
|
||||
// Variables
|
||||
std::vector<color_t> color; // Vector con los colores para el fade
|
||||
int counter; // Contador
|
||||
Uint32 ticks; // Contador de ticks para ajustar la velocidad del programa
|
||||
Uint32 ticksSpeed; // Velocidad a la que se repiten los bucles del programa
|
||||
int initFade; // Tiempo del contador cuando inicia el fade a negro
|
||||
int endLogo; // Tiempo del contador para terminar el logo
|
||||
int postLogo; // Tiempo que dura el logo con el fade al maximo
|
||||
|
||||
// Actualiza las variables
|
||||
void update();
|
||||
|
||||
// Dibuja en pantalla
|
||||
void render();
|
||||
|
||||
// Comprueba el manejador de eventos
|
||||
void checkEvents();
|
||||
|
||||
// Comprueba las entradas
|
||||
void checkInput();
|
||||
|
||||
// Gestiona el logo de JAILGAME
|
||||
void updateJAILGAMES();
|
||||
|
||||
// Gestiona el color de las texturas
|
||||
void updateTextureColors();
|
||||
|
||||
// Cambia la paleta
|
||||
void switchPalette();
|
||||
|
||||
// Termina la sección
|
||||
void endSection();
|
||||
|
||||
public:
|
||||
// Constructor
|
||||
Logo(SDL_Renderer *renderer, Screen *screen, Resource *resource, Asset *asset, Input *input, options_t *options, section_t *section);
|
||||
|
||||
// Destructor
|
||||
~Logo();
|
||||
|
||||
// Bucle principal
|
||||
void run();
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -1,531 +0,0 @@
|
||||
#include "gamestate_title.h"
|
||||
|
||||
// Constructor
|
||||
Title::Title(SDL_Renderer *renderer, Screen *screen, Resource *resource, Asset *asset, Input *input, Online *online, options_t *options, section_t *section)
|
||||
{
|
||||
// Copia la dirección de los objetos
|
||||
this->resource = resource;
|
||||
this->renderer = renderer;
|
||||
this->screen = screen;
|
||||
this->asset = asset;
|
||||
this->input = input;
|
||||
this->online = online;
|
||||
this->options = options;
|
||||
this->section = section;
|
||||
|
||||
// Reserva memoria para los punteros
|
||||
eventHandler = new SDL_Event();
|
||||
cheevos = new Cheevos(screen, options, asset->get("cheevos.bin"), online);
|
||||
if (options->palette == p_zxspectrum)
|
||||
{
|
||||
texture = resource->getTexture("title_logo.png");
|
||||
}
|
||||
else if (options->palette == p_zxarne)
|
||||
{
|
||||
texture = resource->getTexture("title_logo.png");
|
||||
}
|
||||
sprite = new Sprite(0, 0, texture->getWidth(), texture->getHeight(), texture, renderer);
|
||||
text = new Text(resource->getOffset("smb2.txt"), resource->getTexture("smb2.png"), renderer);
|
||||
infoText = new Text(resource->getOffset("subatomic.txt"), resource->getTexture("subatomic.png"), renderer);
|
||||
|
||||
// Crea la textura para los graficos que aparecen en el fondo de la pantalla de titulo
|
||||
bgTexture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, GAMECANVAS_WIDTH, GAMECANVAS_HEIGHT);
|
||||
if (bgTexture == nullptr)
|
||||
{
|
||||
if (options->console)
|
||||
{
|
||||
std::cout << "Error: bgTexture could not be created!\nSDL Error: " << SDL_GetError() << std::endl;
|
||||
}
|
||||
}
|
||||
SDL_SetTextureBlendMode(bgTexture, SDL_BLENDMODE_BLEND);
|
||||
|
||||
// Carga la surface con los gráficos de la pantalla de carga
|
||||
pInit(renderer, 256, 128);
|
||||
loading_screen = pLoadSurface(asset->get("loading_screen_color.gif").c_str());
|
||||
pLoadPal(asset->get("loading_screen_color.gif").c_str());
|
||||
pSetSource(loading_screen);
|
||||
|
||||
// Inicializa variables
|
||||
counter = 0;
|
||||
state = section->subsection == SUBSECTION_TITLE_WITH_LOADING_SCREEN ? show_loading_screen : show_menu;
|
||||
section->name = SECTION_TITLE;
|
||||
section->subsection = 0;
|
||||
ticks = 0;
|
||||
ticksSpeed = 15;
|
||||
marqueeSpeed = 3;
|
||||
initMarquee();
|
||||
showCheevos = false;
|
||||
|
||||
// Crea y rellena la textura para mostrar los logros
|
||||
createCheevosTexture();
|
||||
|
||||
// Cambia el color del borde
|
||||
screen->setBorderColor(stringToColor(options->palette, "black"));
|
||||
|
||||
// Rellena la textura de fondo con todos los gráficos
|
||||
fillTexture();
|
||||
}
|
||||
|
||||
// Destructor
|
||||
Title::~Title()
|
||||
{
|
||||
delete cheevos;
|
||||
delete eventHandler;
|
||||
delete sprite;
|
||||
delete cheevosSprite;
|
||||
delete cheevosTexture;
|
||||
delete text;
|
||||
delete infoText;
|
||||
pDeleteSurface(loading_screen);
|
||||
SDL_DestroyTexture(bgTexture);
|
||||
}
|
||||
|
||||
// Inicializa la marquesina
|
||||
void Title::initMarquee()
|
||||
{
|
||||
letters.clear();
|
||||
longText = "HEY JAILERS!! IT'S 2022 AND WE'RE STILL ROCKING LIKE IT'S 1998!!! HAVE YOU HEARD IT? JAILGAMES ARE BACK!! YEEESSS BACK!! MORE THAN 10 TITLES ON JAILDOC'S KITCHEN!! THATS A LOOOOOOT OF JAILGAMES, BUT WHICH ONE WILL STRIKE FIRST? THERE IS ALSO A NEW DEVICE TO COME THAT WILL BLOW YOUR MIND WITH JAILGAMES ON THE GO: P.A.C.O. BUT WAIT! WHAT'S THAT BEAUTY I'M SEEING RIGHT OVER THERE?? OOOH THAT TINY MINIASCII IS PURE LOVE!! I WANT TO LICK EVERY BYTE OF IT!! OH SHIT! AND DON'T FORGET TO BRING BACK THOSE OLD AND FAT MS-DOS JAILGAMES TO GITHUB TO KEEP THEM ALIVE!! WHAT WILL BE THE NEXT JAILDOC RELEASE? WHAT WILL BE THE NEXT PROJECT TO COME ALIVE?? OH BABY WE DON'T KNOW BUT HERE YOU CAN FIND THE ANSWER, YOU JUST HAVE TO COMPLETE JAILDOCTOR'S DILEMMA ... COULD YOU?";
|
||||
for (int i = 0; i < (int)longText.length(); ++i)
|
||||
{
|
||||
letter_t l;
|
||||
l.letter = longText.substr(i, 1);
|
||||
l.x = 256;
|
||||
l.enabled = false;
|
||||
letters.push_back(l);
|
||||
}
|
||||
letters[0].enabled = true;
|
||||
}
|
||||
|
||||
// Comprueba el manejador de eventos
|
||||
void Title::checkEvents()
|
||||
{
|
||||
// Comprueba los eventos que hay en la cola
|
||||
while (SDL_PollEvent(eventHandler) != 0)
|
||||
{
|
||||
// Evento de salida de la aplicación
|
||||
if (eventHandler->type == SDL_QUIT)
|
||||
{
|
||||
section->name = SECTION_QUIT;
|
||||
break;
|
||||
}
|
||||
|
||||
// Solo se comprueban estas teclas si no está activo el menu de logros
|
||||
if (eventHandler->type == SDL_KEYDOWN)
|
||||
{
|
||||
if (!showCheevos)
|
||||
{
|
||||
switch (eventHandler->key.keysym.scancode)
|
||||
{
|
||||
case SDL_SCANCODE_1:
|
||||
section->name = SECTION_GAME;
|
||||
section->subsection = 0;
|
||||
break;
|
||||
|
||||
case SDL_SCANCODE_2:
|
||||
showCheevos = true;
|
||||
break;
|
||||
|
||||
case SDL_SCANCODE_3:
|
||||
runEnterID();
|
||||
counter = 0;
|
||||
cheevos->reload();
|
||||
fillTexture();
|
||||
createCheevosTexture();
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Comprueba las entradas
|
||||
void Title::checkInput()
|
||||
{
|
||||
if (showCheevos)
|
||||
{
|
||||
if (input->checkInput(input_down, REPEAT_TRUE))
|
||||
{
|
||||
moveCheevosList(1);
|
||||
}
|
||||
else if (input->checkInput(input_up, REPEAT_TRUE))
|
||||
{
|
||||
moveCheevosList(0);
|
||||
}
|
||||
}
|
||||
|
||||
if (input->checkInput(input_exit, REPEAT_FALSE))
|
||||
{
|
||||
if (showCheevos)
|
||||
{
|
||||
hideCheevosList();
|
||||
counter = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
section->name = SECTION_QUIT;
|
||||
}
|
||||
}
|
||||
|
||||
else if (input->checkInput(input_toggle_border, REPEAT_FALSE))
|
||||
{
|
||||
screen->switchBorder();
|
||||
resource->reLoadTextures();
|
||||
}
|
||||
|
||||
else if (input->checkInput(input_window_fullscreen, REPEAT_FALSE))
|
||||
{
|
||||
screen->switchVideoMode();
|
||||
resource->reLoadTextures();
|
||||
}
|
||||
|
||||
else if (input->checkInput(input_window_dec_size, REPEAT_FALSE))
|
||||
{
|
||||
screen->decWindowSize();
|
||||
resource->reLoadTextures();
|
||||
}
|
||||
|
||||
else if (input->checkInput(input_window_inc_size, REPEAT_FALSE))
|
||||
{
|
||||
screen->incWindowSize();
|
||||
resource->reLoadTextures();
|
||||
}
|
||||
|
||||
else if (input->checkInput(input_swap_palette, REPEAT_FALSE))
|
||||
{
|
||||
switchPalette();
|
||||
}
|
||||
|
||||
else if (input->checkInput(input_accept, REPEAT_FALSE) || input->checkInput(input_pause, REPEAT_FALSE))
|
||||
{
|
||||
if (state == show_loading_screen)
|
||||
{
|
||||
state = fade_loading_screen;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Actualiza la marquesina
|
||||
void Title::updateMarquee()
|
||||
{
|
||||
for (int i = 0; i < (int)letters.size(); ++i)
|
||||
{
|
||||
if (letters[i].enabled)
|
||||
{
|
||||
letters[i].x -= marqueeSpeed;
|
||||
if (letters[i].x < -10)
|
||||
{
|
||||
letters[i].enabled = false;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (i > 0 && letters[i - 1].x < 256 && letters[i - 1].enabled)
|
||||
{
|
||||
letters[i].enabled = true;
|
||||
letters[i].x = letters[i - 1].x + text->lenght(letters[i - 1].letter) + 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Comprueba si ha terminado la marquesina y la reinicia
|
||||
if (letters[letters.size() - 1].x < -10)
|
||||
{ // Inicializa la marquesina
|
||||
initMarquee();
|
||||
}
|
||||
}
|
||||
|
||||
// Dibuja la marquesina
|
||||
void Title::renderMarquee()
|
||||
{
|
||||
for (auto l : letters)
|
||||
{
|
||||
if (l.enabled)
|
||||
{
|
||||
text->writeColored(l.x, 184, l.letter, stringToColor(options->palette, "white"));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Dibuja la linea de información inferior
|
||||
void Title::renderInfo()
|
||||
{
|
||||
const std::string loginText = options->online.enabled ? "OnLine: " + options->online.jailerID : "OnLine: OFF";
|
||||
infoText->write(1, 1, loginText);
|
||||
const std::string version = "v.1.09";
|
||||
const int x = GAMECANVAS_WIDTH - infoText->lenght(version) - 1;
|
||||
infoText->write(x, 1, version);
|
||||
}
|
||||
|
||||
// Actualiza las variables
|
||||
void Title::update()
|
||||
{
|
||||
// Comprueba que la diferencia de ticks sea mayor a la velocidad del juego
|
||||
if (SDL_GetTicks() - ticks > ticksSpeed)
|
||||
{
|
||||
// Actualiza el contador de ticks
|
||||
ticks = SDL_GetTicks();
|
||||
|
||||
// Comprueba las entradas
|
||||
checkInput();
|
||||
|
||||
// Actualiza las notificaciones
|
||||
screen->updateNotifier();
|
||||
|
||||
// Incrementa el contador
|
||||
counter++;
|
||||
|
||||
switch (state)
|
||||
{
|
||||
case show_loading_screen:
|
||||
if (counter == 500)
|
||||
{
|
||||
counter = 0;
|
||||
state = fade_loading_screen;
|
||||
}
|
||||
break;
|
||||
|
||||
case fade_loading_screen:
|
||||
if (counter % 4 == 0)
|
||||
if (pFadePal())
|
||||
{
|
||||
counter = 0;
|
||||
state = show_menu;
|
||||
}
|
||||
break;
|
||||
|
||||
case show_menu:
|
||||
// Actualiza la marquesina
|
||||
updateMarquee();
|
||||
|
||||
// Si el contador alcanza cierto valor, termina la seccion
|
||||
if (counter == 2200)
|
||||
{
|
||||
if (!showCheevos)
|
||||
{
|
||||
section->name = SECTION_CREDITS;
|
||||
section->subsection = 0;
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Dibuja en pantalla
|
||||
void Title::render()
|
||||
{
|
||||
// Prepara para empezar a dibujar en la textura de juego
|
||||
screen->start();
|
||||
screen->clean(stringToColor(options->palette, "black"));
|
||||
|
||||
if (state == show_menu)
|
||||
{
|
||||
// Dibuja la textura de fondo
|
||||
SDL_RenderCopy(renderer, bgTexture, nullptr, nullptr);
|
||||
|
||||
// Dibuja la marquesina
|
||||
renderMarquee();
|
||||
|
||||
// Dibuja la información de logros
|
||||
if (showCheevos)
|
||||
{
|
||||
cheevosSprite->render();
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// Dibuja la pantalla de carga
|
||||
pCls(4);
|
||||
pBlit(0, 0, 0, 0, 256, 128);
|
||||
pFlip(renderer);
|
||||
|
||||
// Dibuja el logo del título
|
||||
sprite->render();
|
||||
}
|
||||
|
||||
// Vuelca el contenido del renderizador en pantalla
|
||||
screen->blit();
|
||||
}
|
||||
|
||||
// Bucle para el logo del juego
|
||||
void Title::run()
|
||||
{
|
||||
while (section->name == SECTION_TITLE)
|
||||
{
|
||||
update();
|
||||
checkEvents();
|
||||
render();
|
||||
}
|
||||
}
|
||||
|
||||
// Recarga las texturas
|
||||
void Title::reLoadTextures()
|
||||
{
|
||||
// Carga la textura adecuada
|
||||
if (options->palette == p_zxspectrum)
|
||||
{
|
||||
// texture->loadFromFile(asset->get("loading_screen_color.png"), renderer);
|
||||
texture = resource->getTexture("loading_screen_color.png");
|
||||
}
|
||||
else if (options->palette == p_zxarne)
|
||||
{
|
||||
// texture->loadFromFile(asset->get("loading_screen_color_zxarne.png"), renderer);
|
||||
texture = resource->getTexture("loading_screen_color_zxarne.png");
|
||||
}
|
||||
|
||||
texture->reLoad();
|
||||
}
|
||||
|
||||
// Cambia la paleta
|
||||
void Title::switchPalette()
|
||||
{
|
||||
if (options->palette == p_zxspectrum)
|
||||
{
|
||||
options->palette = p_zxarne;
|
||||
sprite->setTexture(resource->getTexture("loading_screen_color_zxarne.png"));
|
||||
}
|
||||
else
|
||||
{
|
||||
options->palette = p_zxspectrum;
|
||||
sprite->setTexture(resource->getTexture("loading_screen_color.png"));
|
||||
}
|
||||
|
||||
// Cambia el color del borde
|
||||
screen->setBorderColor(stringToColor(options->palette, "bright_blue"));
|
||||
}
|
||||
|
||||
// Desplaza la lista de logros
|
||||
void Title::moveCheevosList(int direction)
|
||||
{
|
||||
const int speed = 2;
|
||||
cheevosTextureView.y = direction == 0 ? cheevosTextureView.y - speed : cheevosTextureView.y + speed;
|
||||
|
||||
const int bottom = cheevosTexture->getHeight() - cheevosTextureView.h;
|
||||
if (cheevosTextureView.y < 0)
|
||||
cheevosTextureView.y = 0;
|
||||
else if (cheevosTextureView.y > bottom)
|
||||
cheevosTextureView.y = bottom;
|
||||
|
||||
cheevosSprite->setSpriteClip(cheevosTextureView);
|
||||
}
|
||||
|
||||
// Ejecuta la seccion en la que se solicita al usuario su ID online
|
||||
void Title::runEnterID()
|
||||
{
|
||||
enterID = new EnterID(renderer, screen, asset, options, section);
|
||||
enterID->run();
|
||||
delete enterID;
|
||||
}
|
||||
|
||||
// Rellena la textura de fondo con todos los gráficos
|
||||
void Title::fillTexture()
|
||||
{
|
||||
// Coloca el puntero del renderizador sobre la textura
|
||||
SDL_SetRenderTarget(renderer, bgTexture);
|
||||
|
||||
// Rellena la textura de color
|
||||
const color_t c = stringToColor(options->palette, "black");
|
||||
SDL_SetRenderDrawColor(renderer, c.r, c.g, c.b, 0xFF);
|
||||
SDL_RenderClear(renderer);
|
||||
|
||||
// Pinta el gráfico del titulo a partir del sprite
|
||||
sprite->render();
|
||||
|
||||
// Borra la firma
|
||||
//const color_t coverColor = stringToColor(options->palette, "black");
|
||||
//SDL_SetRenderDrawColor(renderer, coverColor.r, coverColor.g, coverColor.b, 0xFF);
|
||||
//SDL_Rect coverRect = {28, 11, 21, 5};
|
||||
//SDL_RenderFillRect(renderer, &coverRect);
|
||||
|
||||
// Escribe el texto en la textura
|
||||
const color_t textColor = stringToColor(options->palette, "green");
|
||||
const int textSize = text->getCharacterSize();
|
||||
const std::string onlineText = options->online.jailerID == "" ? "(OFF)" : "(" + options->online.jailerID + ")";
|
||||
text->writeDX(TXT_CENTER | TXT_COLOR, PLAY_AREA_CENTER_X, 11 * textSize, "1.PLAY", 1, textColor);
|
||||
text->writeDX(TXT_CENTER | TXT_COLOR, PLAY_AREA_CENTER_X, 13 * textSize, "2.ACHIEVEMENTS", 1, textColor);
|
||||
text->writeDX(TXT_CENTER | TXT_COLOR, PLAY_AREA_CENTER_X, 15 * textSize, "3.ONLINE MODE", 1, textColor);
|
||||
text->writeDX(TXT_CENTER | TXT_COLOR, PLAY_AREA_CENTER_X, 16 * textSize + 1, onlineText, 1, textColor);
|
||||
text->writeDX(TXT_CENTER | TXT_COLOR, PLAY_AREA_CENTER_X, 20 * textSize, "ESC.EXIT GAME", 1, textColor);
|
||||
|
||||
// Devuelve el puntero del renderizador a su sitio
|
||||
SDL_SetRenderTarget(renderer, nullptr);
|
||||
}
|
||||
|
||||
// Crea y rellena la textura para mostrar los logros
|
||||
void Title::createCheevosTexture()
|
||||
{
|
||||
// Crea la textura con el listado de logros
|
||||
const std::vector<cheevos_t> cheevosList = cheevos->list();
|
||||
// const int iconSize = 16; // Altura del icono que representa a cada logro
|
||||
const int cheevosTextureWidth = 200;
|
||||
const int cheevosTextureViewHeight = 110;
|
||||
const int cheevosTexturePosY = 73;
|
||||
const int cheevosPadding = 10;
|
||||
const int cheevoHeight = cheevosPadding + (infoText->getCharacterSize() * 2) + 1;
|
||||
const int cheevosTextureHeight = (cheevoHeight * cheevosList.size()) + 2 + infoText->getCharacterSize() + 8;
|
||||
cheevosTexture = new Texture(renderer);
|
||||
cheevosTexture->createBlank(renderer, cheevosTextureWidth, cheevosTextureHeight, SDL_TEXTUREACCESS_TARGET);
|
||||
cheevosTexture->setAsRenderTarget(renderer);
|
||||
cheevosTexture->setBlendMode(SDL_BLENDMODE_BLEND);
|
||||
|
||||
// Rellena la textura con color sólido
|
||||
const color_t cheevosBGColor = stringToColor(options->palette, "black");
|
||||
SDL_SetRenderDrawColor(renderer, cheevosBGColor.r, cheevosBGColor.g, cheevosBGColor.b, 0xFF);
|
||||
SDL_RenderClear(renderer);
|
||||
|
||||
// Escribe la lista de logros en la textura
|
||||
const std::string cheevosOwner = options->online.jailerID == "" ? "LOCAL ACHIEVEMENTS" : "ACHIEVEMENTS FOR " + toUpper(options->online.jailerID);
|
||||
const std::string cheevosListCaption = cheevosOwner + " (" + std::to_string(cheevos->unlocked()) + " / " + std::to_string(cheevos->count()) + ")";
|
||||
int pos = 2;
|
||||
infoText->writeDX(TXT_CENTER | TXT_COLOR, cheevosTexture->getWidth() / 2, pos, cheevosListCaption, 1, stringToColor(options->palette, "bright_green"));
|
||||
pos += infoText->getCharacterSize();
|
||||
const color_t cheevoLockedColor = stringToColor(options->palette, "white");
|
||||
const color_t cheevoUnlockedColor = stringToColor(options->palette, "bright_green");
|
||||
color_t cheevoColor;
|
||||
SDL_SetRenderDrawColor(renderer, cheevoLockedColor.r, cheevoLockedColor.g, cheevoLockedColor.b, 0xFF);
|
||||
const int lineX1 = (cheevosTextureWidth / 7) * 3;
|
||||
const int lineX2 = lineX1 + ((cheevosTextureWidth / 7) * 1);
|
||||
|
||||
// Texture *iconTexture = new Texture(renderer, asset->get("notify.png"));
|
||||
// Sprite *sp = new Sprite({0, 0, iconSize, iconSize}, iconTexture, renderer);
|
||||
for (auto cheevo : cheevosList)
|
||||
{
|
||||
cheevoColor = cheevo.completed ? cheevoUnlockedColor : cheevoLockedColor;
|
||||
// sp->setPos({2, pos, iconSize, iconSize});
|
||||
// sp->setSpriteClip({iconSize * 2, 0, iconSize, iconSize});
|
||||
// sp->getTexture()->setColor(cheevoColor.r, cheevoColor.g, cheevoColor.b);
|
||||
// sp->render();
|
||||
pos += cheevosPadding;
|
||||
int half = cheevosPadding / 2;
|
||||
SDL_RenderDrawLine(renderer, lineX1, pos - half - 1, lineX2, pos - half - 1);
|
||||
// infoText->writeColored(2 + iconSize + 2, pos, cheevo.caption, cheevoColor);
|
||||
infoText->writeDX(TXT_CENTER | TXT_COLOR, cheevosTextureWidth / 2, pos, cheevo.caption, 1, cheevoColor);
|
||||
pos += infoText->getCharacterSize() + 1;
|
||||
// infoText->writeColored(2 + iconSize + 2, pos, cheevo.description, cheevoColor);
|
||||
infoText->writeDX(TXT_CENTER | TXT_COLOR, cheevosTextureWidth / 2, pos, cheevo.description, 1, cheevoColor);
|
||||
// pos += cheevosPadding;
|
||||
pos += infoText->getCharacterSize();
|
||||
}
|
||||
// delete sp;
|
||||
// delete iconTexture;
|
||||
|
||||
// Crea el sprite para el listado de logros
|
||||
// cheevosSprite = new Sprite((GAMECANVAS_WIDTH - cheevosTexture->getWidth()) / 2, (GAMECANVAS_HEIGHT - cheevosTextureViewHeight) / 2, cheevosTexture->getWidth(), cheevosTexture->getHeight(), cheevosTexture, renderer);
|
||||
cheevosSprite = new Sprite((GAMECANVAS_WIDTH - cheevosTexture->getWidth()) / 2, cheevosTexturePosY, cheevosTexture->getWidth(), cheevosTexture->getHeight(), cheevosTexture, renderer);
|
||||
cheevosTextureView = {0, 0, cheevosTexture->getWidth(), cheevosTextureViewHeight};
|
||||
cheevosSprite->setSpriteClip(cheevosTextureView);
|
||||
}
|
||||
|
||||
// Oculta la lista de logros
|
||||
void Title::hideCheevosList()
|
||||
{
|
||||
showCheevos = false;
|
||||
cheevosTextureView.y = 0;
|
||||
cheevosSprite->setSpriteClip(cheevosTextureView);
|
||||
}
|
||||
@@ -1,127 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include <SDL2/SDL.h>
|
||||
#include "cheevos.h"
|
||||
#include "enter_id.h"
|
||||
#include "jail_engine/asset.h"
|
||||
#include "jail_engine/input.h"
|
||||
#include "jail_engine/jail_audio.h"
|
||||
#include "jail_engine/resource.h"
|
||||
#include "jail_engine/paleta.h"
|
||||
#include "jail_engine/screen.h"
|
||||
#include "jail_engine/sprite.h"
|
||||
#include "jail_engine/text.h"
|
||||
#include "jail_engine/utils.h"
|
||||
#include "online.h"
|
||||
#include "const.h"
|
||||
#include <vector>
|
||||
|
||||
#ifndef TITLE_H
|
||||
#define TITLE_H
|
||||
|
||||
class Title
|
||||
{
|
||||
private:
|
||||
struct letter_t
|
||||
{
|
||||
std::string letter; // Letra a escribir
|
||||
int x; // Posición en el eje x
|
||||
bool enabled; // Solo se escriben y mueven si estan habilitadas
|
||||
};
|
||||
|
||||
enum states_e
|
||||
{
|
||||
show_loading_screen,
|
||||
fade_loading_screen,
|
||||
show_menu
|
||||
};
|
||||
|
||||
// Objetos y punteros
|
||||
SDL_Renderer *renderer; // El renderizador de la ventana
|
||||
Screen *screen; // Objeto encargado de dibujar en pantalla
|
||||
Resource *resource; // Objeto con los recursos
|
||||
Asset *asset; // Objeto con los ficheros de recursos
|
||||
Input *input; // Objeto pata gestionar la entrada
|
||||
Online *online; // Objeto para gestionar la lectura y escritura de datos en el servidor remoto
|
||||
SDL_Event *eventHandler; // Manejador de eventos
|
||||
Texture *texture; // Textura con los graficos
|
||||
Sprite *sprite; // Sprite para manejar la textura
|
||||
SDL_Texture *bgTexture; // Textura para dibujar el fondo de la pantalla
|
||||
Text *text; // Objeto para escribir texto en pantalla
|
||||
Text *infoText; // Objeto para escribir texto en pantalla
|
||||
options_t *options; // Puntero a las opciones del juego
|
||||
Texture *cheevosTexture; // Textura con lo lista de logros
|
||||
Sprite *cheevosSprite; // Sprite para manejar la textura con la lista de logros
|
||||
Cheevos *cheevos; // Objeto encargado de gestionar los logros del juego
|
||||
EnterID *enterID; // Objeto para recoger el JailerID desde el teclado
|
||||
section_t *section; // Estado del bucle principal para saber si continua o se sale
|
||||
|
||||
// Variables
|
||||
int counter; // Contador
|
||||
std::string longText; // Texto que aparece en la parte inferior del titulo
|
||||
Uint32 ticks; // Contador de ticks para ajustar la velocidad del programa
|
||||
Uint32 ticksSpeed; // Velocidad a la que se repiten los bucles del programa
|
||||
std::vector<letter_t> letters; // Vector con las letras de la marquesina
|
||||
int marqueeSpeed; // Velocidad de desplazamiento de la marquesina
|
||||
bool showCheevos; // Indica si se muestra por pantalla el listado de logros
|
||||
SDL_Rect cheevosTextureView; // Zona visible de la textura con el listado de logros
|
||||
states_e state; // Estado en el que se encuentra el bucle principal
|
||||
jSurface loading_screen; // Surface con los gráficos de la pantalla de carga
|
||||
|
||||
// Actualiza las variables
|
||||
void update();
|
||||
|
||||
// Dibuja en pantalla
|
||||
void render();
|
||||
|
||||
// Comprueba el manejador de eventos
|
||||
void checkEvents();
|
||||
|
||||
// Comprueba las entradas
|
||||
void checkInput();
|
||||
|
||||
// Inicializa la marquesina
|
||||
void initMarquee();
|
||||
|
||||
// Actualiza la marquesina
|
||||
void updateMarquee();
|
||||
|
||||
// Dibuja la marquesina
|
||||
void renderMarquee();
|
||||
|
||||
// Dibuja la linea de información inferior
|
||||
void renderInfo();
|
||||
|
||||
// Recarga las texturas
|
||||
void reLoadTextures();
|
||||
|
||||
// Cambia la paleta
|
||||
void switchPalette();
|
||||
|
||||
// Desplaza la lista de logros
|
||||
void moveCheevosList(int direction);
|
||||
|
||||
// Ejecuta la seccion en la que se solicita al usuario su ID online
|
||||
void runEnterID();
|
||||
|
||||
// Rellena la textura de fondo con todos los gráficos
|
||||
void fillTexture();
|
||||
|
||||
// Crea y rellena la textura para mostrar los logros
|
||||
void createCheevosTexture();
|
||||
|
||||
// Oculta la lista de logros
|
||||
void hideCheevosList();
|
||||
|
||||
public:
|
||||
// Constructor
|
||||
Title(SDL_Renderer *renderer, Screen *screen, Resource *resource, Asset *asset, Input *input, Online *online, options_t *options, section_t *section);
|
||||
|
||||
// Destructor
|
||||
~Title();
|
||||
|
||||
// Bucle principal
|
||||
void run();
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -1,31 +1,52 @@
|
||||
#include "input.h"
|
||||
#include <iostream>
|
||||
#include <SDL2/SDL.h> // Para SDL_INIT_GAMECONTROLLER, SDL_InitSubS...
|
||||
#include <SDL2/SDL_error.h> // Para SDL_GetError
|
||||
#include <SDL2/SDL_events.h> // Para SDL_ENABLE
|
||||
#include <SDL2/SDL_joystick.h> // Para SDL_NumJoysticks
|
||||
#include <SDL2/SDL_keyboard.h> // Para SDL_GetKeyboardState
|
||||
#include <iostream> // Para basic_ostream, operator<<, cout, basi...
|
||||
|
||||
// [SINGLETON]
|
||||
Input *Input::input_ = nullptr;
|
||||
|
||||
// [SINGLETON] Crearemos el objeto con esta función estática
|
||||
void Input::init(const std::string &game_controller_db_path)
|
||||
{
|
||||
Input::input_ = new Input(game_controller_db_path);
|
||||
}
|
||||
|
||||
// [SINGLETON] Destruiremos el objeto con esta función estática
|
||||
void Input::destroy()
|
||||
{
|
||||
delete Input::input_;
|
||||
}
|
||||
|
||||
// [SINGLETON] Con este método obtenemos el objeto y podemos trabajar con él
|
||||
Input *Input::get()
|
||||
{
|
||||
return Input::input_;
|
||||
}
|
||||
|
||||
// Constructor
|
||||
Input::Input(std::string file)
|
||||
Input::Input(const std::string &game_controller_db_path)
|
||||
: db_path_(game_controller_db_path)
|
||||
{
|
||||
// Fichero gamecontrollerdb.txt
|
||||
dbPath = file;
|
||||
|
||||
// Inicializa las variables
|
||||
keyBindings_t kb;
|
||||
kb.scancode = 0;
|
||||
kb.active = false;
|
||||
keyBindings.resize(input_number_of_inputs, kb);
|
||||
key_bindings_.resize(input_number_of_inputs, kb);
|
||||
|
||||
GameControllerBindings_t gcb;
|
||||
gcb.button = SDL_CONTROLLER_BUTTON_INVALID;
|
||||
gcb.active = false;
|
||||
gameControllerBindings.resize(input_number_of_inputs, gcb);
|
||||
|
||||
verbose = true;
|
||||
enabled = true;
|
||||
game_controller_bindings_.resize(input_number_of_inputs, gcb);
|
||||
}
|
||||
|
||||
// Actualiza el estado del objeto
|
||||
void Input::update()
|
||||
{
|
||||
if (disabledUntil == d_keyPressed && !checkAnyInput())
|
||||
if (disabled_until_ == d_keyPressed && !checkAnyInput())
|
||||
{
|
||||
enable();
|
||||
}
|
||||
@@ -34,19 +55,19 @@ void Input::update()
|
||||
// Asigna inputs a teclas
|
||||
void Input::bindKey(Uint8 input, SDL_Scancode code)
|
||||
{
|
||||
keyBindings[input].scancode = code;
|
||||
key_bindings_[input].scancode = code;
|
||||
}
|
||||
|
||||
// Asigna inputs a botones del mando
|
||||
void Input::bindGameControllerButton(Uint8 input, SDL_GameControllerButton button)
|
||||
{
|
||||
gameControllerBindings[input].button = button;
|
||||
game_controller_bindings_[input].button = button;
|
||||
}
|
||||
|
||||
// Comprueba si un input esta activo
|
||||
bool Input::checkInput(Uint8 input, bool repeat, int device, int index)
|
||||
{
|
||||
if (!enabled)
|
||||
if (!enabled_)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
@@ -65,7 +86,7 @@ bool Input::checkInput(Uint8 input, bool repeat, int device, int index)
|
||||
|
||||
if (repeat)
|
||||
{
|
||||
if (keyStates[keyBindings[input].scancode] != 0)
|
||||
if (keyStates[key_bindings_[input].scancode] != 0)
|
||||
{
|
||||
successKeyboard = true;
|
||||
}
|
||||
@@ -76,11 +97,11 @@ bool Input::checkInput(Uint8 input, bool repeat, int device, int index)
|
||||
}
|
||||
else
|
||||
{
|
||||
if (!keyBindings[input].active)
|
||||
if (!key_bindings_[input].active)
|
||||
{
|
||||
if (keyStates[keyBindings[input].scancode] != 0)
|
||||
if (keyStates[key_bindings_[input].scancode] != 0)
|
||||
{
|
||||
keyBindings[input].active = true;
|
||||
key_bindings_[input].active = true;
|
||||
successKeyboard = true;
|
||||
}
|
||||
else
|
||||
@@ -90,9 +111,9 @@ bool Input::checkInput(Uint8 input, bool repeat, int device, int index)
|
||||
}
|
||||
else
|
||||
{
|
||||
if (keyStates[keyBindings[input].scancode] == 0)
|
||||
if (keyStates[key_bindings_[input].scancode] == 0)
|
||||
{
|
||||
keyBindings[input].active = false;
|
||||
key_bindings_[input].active = false;
|
||||
successKeyboard = false;
|
||||
}
|
||||
else
|
||||
@@ -108,7 +129,7 @@ bool Input::checkInput(Uint8 input, bool repeat, int device, int index)
|
||||
{
|
||||
if (repeat)
|
||||
{
|
||||
if (SDL_GameControllerGetButton(connectedControllers[index], gameControllerBindings[input].button) != 0)
|
||||
if (SDL_GameControllerGetButton(connected_controllers_[index], game_controller_bindings_[input].button) != 0)
|
||||
{
|
||||
successGameController = true;
|
||||
}
|
||||
@@ -119,11 +140,11 @@ bool Input::checkInput(Uint8 input, bool repeat, int device, int index)
|
||||
}
|
||||
else
|
||||
{
|
||||
if (!gameControllerBindings[input].active)
|
||||
if (!game_controller_bindings_[input].active)
|
||||
{
|
||||
if (SDL_GameControllerGetButton(connectedControllers[index], gameControllerBindings[input].button) != 0)
|
||||
if (SDL_GameControllerGetButton(connected_controllers_[index], game_controller_bindings_[input].button) != 0)
|
||||
{
|
||||
gameControllerBindings[input].active = true;
|
||||
game_controller_bindings_[input].active = true;
|
||||
successGameController = true;
|
||||
}
|
||||
else
|
||||
@@ -133,9 +154,9 @@ bool Input::checkInput(Uint8 input, bool repeat, int device, int index)
|
||||
}
|
||||
else
|
||||
{
|
||||
if (SDL_GameControllerGetButton(connectedControllers[index], gameControllerBindings[input].button) == 0)
|
||||
if (SDL_GameControllerGetButton(connected_controllers_[index], game_controller_bindings_[input].button) == 0)
|
||||
{
|
||||
gameControllerBindings[input].active = false;
|
||||
game_controller_bindings_[input].active = false;
|
||||
successGameController = false;
|
||||
}
|
||||
else
|
||||
@@ -161,9 +182,9 @@ bool Input::checkAnyInput(int device, int index)
|
||||
{
|
||||
const Uint8 *mKeystates = SDL_GetKeyboardState(nullptr);
|
||||
|
||||
for (int i = 0; i < (int)keyBindings.size(); ++i)
|
||||
for (int i = 0; i < (int)key_bindings_.size(); ++i)
|
||||
{
|
||||
if (mKeystates[keyBindings[i].scancode] != 0)
|
||||
if (mKeystates[key_bindings_[i].scancode] != 0)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
@@ -174,9 +195,9 @@ bool Input::checkAnyInput(int device, int index)
|
||||
{
|
||||
if (device == INPUT_USE_GAMECONTROLLER || device == INPUT_USE_ANY)
|
||||
{
|
||||
for (int i = 0; i < (int)gameControllerBindings.size(); ++i)
|
||||
for (int i = 0; i < (int)game_controller_bindings_.size(); ++i)
|
||||
{
|
||||
if (SDL_GameControllerGetButton(connectedControllers[index], gameControllerBindings[i].button) != 0)
|
||||
if (SDL_GameControllerGetButton(connected_controllers_[index], game_controller_bindings_[i].button) != 0)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
@@ -197,56 +218,56 @@ bool Input::discoverGameController()
|
||||
SDL_InitSubSystem(SDL_INIT_GAMECONTROLLER);
|
||||
}
|
||||
|
||||
if (SDL_GameControllerAddMappingsFromFile(dbPath.c_str()) < 0)
|
||||
if (SDL_GameControllerAddMappingsFromFile(db_path_.c_str()) < 0)
|
||||
{
|
||||
if (verbose)
|
||||
if (verbose_)
|
||||
{
|
||||
std::cout << "Error, could not load " << dbPath.c_str() << " file: " << SDL_GetError() << std::endl;
|
||||
std::cout << "Error, could not load " << db_path_.c_str() << " file: " << SDL_GetError() << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
const int nJoysticks = SDL_NumJoysticks();
|
||||
numGamepads = 0;
|
||||
num_gamepads_ = 0;
|
||||
|
||||
// Cuenta el numero de mandos
|
||||
for (int i = 0; i < nJoysticks; ++i)
|
||||
{
|
||||
if (SDL_IsGameController(i))
|
||||
{
|
||||
numGamepads++;
|
||||
num_gamepads_++;
|
||||
}
|
||||
}
|
||||
|
||||
if (verbose)
|
||||
if (verbose_)
|
||||
{
|
||||
std::cout << "\nChecking for game controllers...\n";
|
||||
std::cout << nJoysticks << " joysticks found, " << numGamepads << " are gamepads\n";
|
||||
std::cout << nJoysticks << " joysticks found, " << num_gamepads_ << " are gamepads\n";
|
||||
}
|
||||
|
||||
if (numGamepads > 0)
|
||||
if (num_gamepads_ > 0)
|
||||
{
|
||||
found = true;
|
||||
|
||||
for (int i = 0; i < numGamepads; i++)
|
||||
for (int i = 0; i < num_gamepads_; i++)
|
||||
{
|
||||
// Abre el mando y lo añade a la lista
|
||||
SDL_GameController *pad = SDL_GameControllerOpen(i);
|
||||
if (SDL_GameControllerGetAttached(pad) == 1)
|
||||
{
|
||||
connectedControllers.push_back(pad);
|
||||
connected_controllers_.push_back(pad);
|
||||
const std::string separator(" #");
|
||||
std::string name = SDL_GameControllerNameForIndex(i);
|
||||
name.resize(25);
|
||||
name = name + separator + std::to_string(i);
|
||||
if (verbose)
|
||||
if (verbose_)
|
||||
{
|
||||
std::cout << name << std::endl;
|
||||
}
|
||||
controllerNames.push_back(name);
|
||||
controller_names_.push_back(name);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (verbose)
|
||||
if (verbose_)
|
||||
{
|
||||
std::cout << "SDL_GetError() = " << SDL_GetError() << std::endl;
|
||||
}
|
||||
@@ -262,7 +283,7 @@ bool Input::discoverGameController()
|
||||
// Comprueba si hay algun mando conectado
|
||||
bool Input::gameControllerFound()
|
||||
{
|
||||
if (numGamepads > 0)
|
||||
if (num_gamepads_ > 0)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
@@ -275,9 +296,9 @@ bool Input::gameControllerFound()
|
||||
// Obten el nombre de un mando de juego
|
||||
std::string Input::getControllerName(int index)
|
||||
{
|
||||
if (numGamepads > 0)
|
||||
if (num_gamepads_ > 0)
|
||||
{
|
||||
return controllerNames[index];
|
||||
return controller_names_[index];
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -288,25 +309,25 @@ std::string Input::getControllerName(int index)
|
||||
// Obten el numero de mandos conectados
|
||||
int Input::getNumControllers()
|
||||
{
|
||||
return numGamepads;
|
||||
return num_gamepads_;
|
||||
}
|
||||
|
||||
// Establece si ha de mostrar mensajes
|
||||
void Input::setVerbose(bool value)
|
||||
{
|
||||
verbose = value;
|
||||
verbose_ = value;
|
||||
}
|
||||
|
||||
// Deshabilita las entradas durante un periodo de tiempo
|
||||
void Input::disableUntil(i_disable_e value)
|
||||
{
|
||||
disabledUntil = value;
|
||||
enabled = false;
|
||||
disabled_until_ = value;
|
||||
enabled_ = false;
|
||||
}
|
||||
|
||||
// Hablita las entradas
|
||||
void Input::enable()
|
||||
{
|
||||
enabled = true;
|
||||
disabledUntil = d_notDisabled;
|
||||
enabled_ = true;
|
||||
disabled_until_ = d_notDisabled;
|
||||
}
|
||||
134
source/input.h
Normal file
134
source/input.h
Normal file
@@ -0,0 +1,134 @@
|
||||
#pragma once
|
||||
|
||||
#include <SDL2/SDL_gamecontroller.h> // Para SDL_GameControllerButton, SDL_G...
|
||||
#include <SDL2/SDL_scancode.h> // Para SDL_Scancode
|
||||
#include <SDL2/SDL_stdinc.h> // Para Uint8
|
||||
#include <string> // Para string, basic_string
|
||||
#include <vector> // Para vector
|
||||
|
||||
// Definiciones de repetición
|
||||
constexpr bool REPEAT_TRUE = true;
|
||||
constexpr bool REPEAT_FALSE = false;
|
||||
|
||||
// Tipos de entrada
|
||||
constexpr int INPUT_USE_KEYBOARD = 0;
|
||||
constexpr int INPUT_USE_GAMECONTROLLER = 1;
|
||||
constexpr int INPUT_USE_ANY = 2;
|
||||
|
||||
enum inputs_e
|
||||
{
|
||||
// Inputs obligatorios
|
||||
input_null,
|
||||
input_up,
|
||||
input_down,
|
||||
input_left,
|
||||
input_right,
|
||||
input_pause,
|
||||
input_exit,
|
||||
input_accept,
|
||||
input_cancel,
|
||||
|
||||
// Inputs personalizados
|
||||
input_jump,
|
||||
input_window_inc_size,
|
||||
input_window_dec_size,
|
||||
input_toggle_videomode,
|
||||
input_toggle_border,
|
||||
input_toggle_music,
|
||||
input_toggle_palette,
|
||||
input_toggle_shaders,
|
||||
|
||||
// Input obligatorio
|
||||
input_number_of_inputs
|
||||
};
|
||||
|
||||
enum i_disable_e
|
||||
{
|
||||
d_notDisabled,
|
||||
d_forever,
|
||||
d_keyPressed
|
||||
};
|
||||
|
||||
class Input
|
||||
{
|
||||
private:
|
||||
// [SINGLETON] Objeto privado
|
||||
static Input *input_;
|
||||
|
||||
struct keyBindings_t
|
||||
{
|
||||
Uint8 scancode; // Scancode asociado
|
||||
bool active; // Indica si está activo
|
||||
};
|
||||
|
||||
struct GameControllerBindings_t
|
||||
{
|
||||
SDL_GameControllerButton button; // GameControllerButton asociado
|
||||
bool active; // Indica si está activo
|
||||
};
|
||||
|
||||
// Objetos y punteros
|
||||
std::vector<SDL_GameController *> connected_controllers_; // Vector con todos los mandos conectados
|
||||
|
||||
// Variables
|
||||
std::vector<keyBindings_t> key_bindings_; // Vector con las teclas asociadas a los inputs predefinidos
|
||||
std::vector<GameControllerBindings_t> game_controller_bindings_; // Vector con las teclas asociadas a los inputs predefinidos
|
||||
std::vector<std::string> controller_names_; // Vector con los nombres de los mandos
|
||||
int num_gamepads_; // Numero de mandos conectados
|
||||
std::string db_path_; // Ruta al archivo gamecontrollerdb.txt
|
||||
bool verbose_ = true; // Indica si ha de mostrar mensajes
|
||||
i_disable_e disabled_until_; // Tiempo que esta deshabilitado
|
||||
bool enabled_ = true; // Indica si está habilitado
|
||||
|
||||
// Constructor
|
||||
explicit Input(const std::string &game_controller_db_path);
|
||||
|
||||
// Destructor
|
||||
~Input() = default;
|
||||
|
||||
public:
|
||||
// [SINGLETON] Crearemos el objeto con esta función estática
|
||||
static void init(const std::string &game_controller_db_path);
|
||||
|
||||
// [SINGLETON] Destruiremos el objeto con esta función estática
|
||||
static void destroy();
|
||||
|
||||
// [SINGLETON] Con este método obtenemos el objeto y podemos trabajar con él
|
||||
static Input *get();
|
||||
|
||||
// Actualiza el estado del objeto
|
||||
void update();
|
||||
|
||||
// Asigna inputs a teclas
|
||||
void bindKey(Uint8 input, SDL_Scancode code);
|
||||
|
||||
// Asigna inputs a botones del mando
|
||||
void bindGameControllerButton(Uint8 input, SDL_GameControllerButton button);
|
||||
|
||||
// Comprueba si un input esta activo
|
||||
bool checkInput(Uint8 input, bool repeat = true, int device = INPUT_USE_ANY, int index = 0);
|
||||
|
||||
// Comprueba si hay almenos un input activo
|
||||
bool checkAnyInput(int device = INPUT_USE_ANY, int index = 0);
|
||||
|
||||
// Busca si hay un mando conectado
|
||||
bool discoverGameController();
|
||||
|
||||
// Comprueba si hay algun mando conectado
|
||||
bool gameControllerFound();
|
||||
|
||||
// Obten el numero de mandos conectados
|
||||
int getNumControllers();
|
||||
|
||||
// Obten el nombre de un mando de juego
|
||||
std::string getControllerName(int index);
|
||||
|
||||
// Establece si ha de mostrar mensajes
|
||||
void setVerbose(bool value);
|
||||
|
||||
// Deshabilita las entradas durante un periodo de tiempo
|
||||
void disableUntil(i_disable_e value);
|
||||
|
||||
// Hablita las entradas
|
||||
void enable();
|
||||
};
|
||||
@@ -1,6 +1,6 @@
|
||||
#include "item.h"
|
||||
#include <fstream>
|
||||
#include <sstream>
|
||||
#include "sprite.h" // Para Sprite
|
||||
#include "texture.h" // Para Texture
|
||||
|
||||
// Constructor
|
||||
Item::Item(item_t item)
|
||||
|
||||
@@ -1,25 +1,27 @@
|
||||
#pragma once
|
||||
|
||||
#include <SDL2/SDL.h>
|
||||
#include "jail_engine/asset.h"
|
||||
#include "jail_engine/sprite.h"
|
||||
#include "jail_engine/utils.h"
|
||||
#include <string>
|
||||
|
||||
#ifndef ITEM_H
|
||||
#define ITEM_H
|
||||
#include <SDL2/SDL_rect.h> // Para SDL_Rect, SDL_Point
|
||||
#include <SDL2/SDL_render.h> // Para SDL_Renderer
|
||||
#include <string> // Para basic_string, string
|
||||
#include <vector> // Para vector
|
||||
#include "utils.h" // Para color_t
|
||||
class Sprite;
|
||||
class Texture;
|
||||
|
||||
struct item_t
|
||||
{
|
||||
SDL_Renderer *renderer; // El renderizador de la ventana
|
||||
Texture *texture; // Textura con los graficos del item
|
||||
std::string tileSetFile; // Ruta al fichero con los graficos del item
|
||||
int x; // Posicion del item en pantalla
|
||||
int y; // Posicion del item en pantalla
|
||||
int tile; // Numero de tile dentro de la textura
|
||||
Texture *texture; // Textura con los gráficos del item
|
||||
std::string tileSetFile; // Ruta al fichero con los gráficos del item
|
||||
int x; // Posición del item en pantalla
|
||||
int y; // Posición del item en pantalla
|
||||
int tile; // Número de tile dentro de la textura
|
||||
int counter; // Contador inicial. Es el que lo hace cambiar de color
|
||||
color_t color1; // Uno de los dos colores que se utiliza para el item
|
||||
color_t color2; // Uno de los dos colores que se utiliza para el item
|
||||
|
||||
// Constructor por defecto
|
||||
item_t() : renderer(nullptr), texture(nullptr), x(0), y(0), tile(0), counter(0), color1(), color2() {}
|
||||
};
|
||||
|
||||
class Item
|
||||
@@ -58,6 +60,4 @@ public:
|
||||
|
||||
// Asigna los colores del objeto
|
||||
void setColors(color_t col1, color_t col2);
|
||||
};
|
||||
|
||||
#endif
|
||||
};
|
||||
@@ -71,7 +71,7 @@ int ItemTracker::findByPos(int index, SDL_Point pos)
|
||||
{
|
||||
int i = 0;
|
||||
|
||||
for (auto l:list[index].pos)
|
||||
for (auto l : list[index].pos)
|
||||
{
|
||||
if ((l.x == pos.x) && (l.y == pos.y))
|
||||
{
|
||||
|
||||
@@ -1,11 +1,8 @@
|
||||
#pragma once
|
||||
#include <SDL2/SDL.h>
|
||||
#include "jail_engine/utils.h"
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#ifndef ITEM_TRACKER_H
|
||||
#define ITEM_TRACKER_H
|
||||
#include <SDL2/SDL_rect.h> // Para SDL_Point
|
||||
#include <string> // Para string, basic_string
|
||||
#include <vector> // Para vector
|
||||
|
||||
struct item_tracker_t
|
||||
{
|
||||
@@ -34,6 +31,4 @@ public:
|
||||
|
||||
// Añade el objeto a la lista de objetos cogidos
|
||||
void addItem(std::string name, SDL_Point pos);
|
||||
};
|
||||
|
||||
#endif
|
||||
};
|
||||
479
source/jail_audio.cpp
Normal file
479
source/jail_audio.cpp
Normal file
@@ -0,0 +1,479 @@
|
||||
#include "jail_audio.h"
|
||||
#include <SDL2/SDL_rwops.h> // for SDL_RWFromMem
|
||||
#include <SDL2/SDL_timer.h> // for SDL_GetTicks
|
||||
#include <stdint.h> // for uint8_t, uint32_t
|
||||
#include <stdio.h> // for NULL, fseek, fclose, fopen, fread, ftell
|
||||
#include <stdlib.h> // for free, malloc
|
||||
#include "stb_vorbis.c" // for stb_vorbis_decode_memory
|
||||
|
||||
constexpr int JA_MAX_SIMULTANEOUS_CHANNELS = 20;
|
||||
|
||||
struct JA_Sound_t
|
||||
{
|
||||
Uint32 length{0};
|
||||
Uint8 *buffer{NULL};
|
||||
};
|
||||
|
||||
struct JA_Channel_t
|
||||
{
|
||||
JA_Sound_t *sound;
|
||||
int pos{0};
|
||||
int times{0};
|
||||
JA_Channel_state state{JA_CHANNEL_FREE};
|
||||
};
|
||||
|
||||
struct JA_Music_t
|
||||
{
|
||||
int samples{0};
|
||||
Uint32 length{0};
|
||||
int pos{0};
|
||||
int times{0};
|
||||
short *output{NULL};
|
||||
JA_Music_state state{JA_MUSIC_INVALID};
|
||||
};
|
||||
|
||||
JA_Music_t *current_music{NULL};
|
||||
JA_Channel_t channels[JA_MAX_SIMULTANEOUS_CHANNELS];
|
||||
|
||||
int JA_freq{48000};
|
||||
SDL_AudioFormat JA_format{AUDIO_S16};
|
||||
Uint8 JA_channels{2};
|
||||
int JA_musicVolume = 128;
|
||||
int JA_soundVolume = 64;
|
||||
bool JA_musicEnabled = true;
|
||||
bool JA_soundEnabled = true;
|
||||
SDL_AudioDeviceID sdlAudioDevice = 0;
|
||||
|
||||
bool fading = false;
|
||||
int fade_start_time;
|
||||
int fade_duration;
|
||||
int fade_initial_volume;
|
||||
|
||||
void audioCallback(void *userdata, uint8_t *stream, int len)
|
||||
{
|
||||
SDL_memset(stream, 0, len);
|
||||
if (current_music != NULL && current_music->state == JA_MUSIC_PLAYING)
|
||||
{
|
||||
int volume = JA_musicVolume;
|
||||
if (fading)
|
||||
{
|
||||
int time = SDL_GetTicks();
|
||||
if (time > (fade_start_time + fade_duration))
|
||||
{
|
||||
fading = false;
|
||||
current_music->pos = 0;
|
||||
current_music->state = JA_MUSIC_STOPPED;
|
||||
volume = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
const int time_passed = time - fade_start_time;
|
||||
const float percent = (float)time_passed / (float)fade_duration;
|
||||
volume = JA_musicVolume * (1.0 - percent);
|
||||
}
|
||||
}
|
||||
const int size = SDL_min(len, current_music->length - current_music->pos);
|
||||
SDL_MixAudioFormat(stream, (Uint8 *)(current_music->output) + current_music->pos, AUDIO_S16, size, volume);
|
||||
current_music->pos += size;
|
||||
if (size < len)
|
||||
{
|
||||
if (current_music->times != 0)
|
||||
{
|
||||
SDL_MixAudioFormat(stream + size, (Uint8 *)current_music->output, AUDIO_S16, len - size, volume);
|
||||
current_music->pos = len - size;
|
||||
if (current_music->times > 0)
|
||||
current_music->times--;
|
||||
}
|
||||
else
|
||||
{
|
||||
current_music->pos = 0;
|
||||
current_music->state = JA_MUSIC_STOPPED;
|
||||
}
|
||||
}
|
||||
}
|
||||
// Mixar els channels mi amol
|
||||
for (int i = 0; i < JA_MAX_SIMULTANEOUS_CHANNELS; i++)
|
||||
{
|
||||
if (channels[i].state == JA_CHANNEL_PLAYING)
|
||||
{
|
||||
const int size = SDL_min(len, channels[i].sound->length - channels[i].pos);
|
||||
SDL_MixAudioFormat(stream, channels[i].sound->buffer + channels[i].pos, AUDIO_S16, size, JA_soundVolume);
|
||||
channels[i].pos += size;
|
||||
if (size < len)
|
||||
{
|
||||
if (channels[i].times != 0)
|
||||
{
|
||||
SDL_MixAudioFormat(stream + size, channels[i].sound->buffer, AUDIO_S16, len - size, JA_soundVolume);
|
||||
channels[i].pos = len - size;
|
||||
if (channels[i].times > 0)
|
||||
channels[i].times--;
|
||||
}
|
||||
else
|
||||
{
|
||||
JA_StopChannel(i);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void JA_Init(const int freq, const SDL_AudioFormat format, const int channels)
|
||||
{
|
||||
JA_freq = freq;
|
||||
JA_format = format;
|
||||
JA_channels = channels;
|
||||
SDL_AudioSpec audioSpec{JA_freq, JA_format, JA_channels, 0, 1024, 0, 0, audioCallback, NULL};
|
||||
if (sdlAudioDevice != 0)
|
||||
SDL_CloseAudioDevice(sdlAudioDevice);
|
||||
sdlAudioDevice = SDL_OpenAudioDevice(NULL, 0, &audioSpec, NULL, 0);
|
||||
SDL_PauseAudioDevice(sdlAudioDevice, 0);
|
||||
}
|
||||
|
||||
void JA_Quit()
|
||||
{
|
||||
SDL_PauseAudioDevice(sdlAudioDevice, 1);
|
||||
if (sdlAudioDevice != 0)
|
||||
SDL_CloseAudioDevice(sdlAudioDevice);
|
||||
sdlAudioDevice = 0;
|
||||
}
|
||||
|
||||
JA_Music_t *JA_LoadMusic(Uint8 *buffer, Uint32 length)
|
||||
{
|
||||
int chan, samplerate;
|
||||
JA_Music_t *music = new JA_Music_t();
|
||||
|
||||
music->samples = stb_vorbis_decode_memory(buffer, length, &chan, &samplerate, &music->output);
|
||||
|
||||
SDL_AudioCVT cvt;
|
||||
SDL_BuildAudioCVT(&cvt, AUDIO_S16, chan, samplerate, JA_format, JA_channels, JA_freq);
|
||||
if (cvt.needed)
|
||||
{
|
||||
cvt.len = music->samples * chan * 2;
|
||||
music->length = cvt.len;
|
||||
cvt.buf = (Uint8 *)SDL_malloc(cvt.len * cvt.len_mult);
|
||||
SDL_memcpy(cvt.buf, music->output, cvt.len);
|
||||
SDL_ConvertAudio(&cvt);
|
||||
free(music->output);
|
||||
music->output = (short *)cvt.buf;
|
||||
}
|
||||
music->length = music->samples * chan * 2;
|
||||
music->pos = 0;
|
||||
music->state = JA_MUSIC_STOPPED;
|
||||
|
||||
return music;
|
||||
}
|
||||
|
||||
JA_Music_t *JA_LoadMusic(const char *filename)
|
||||
{
|
||||
// [RZC 28/08/22] Carreguem primer el arxiu en memòria i després el descomprimim. Es algo més rapid.
|
||||
FILE *f = fopen(filename, "rb");
|
||||
fseek(f, 0, SEEK_END);
|
||||
long fsize = ftell(f);
|
||||
fseek(f, 0, SEEK_SET);
|
||||
Uint8 *buffer = (Uint8 *)malloc(fsize + 1);
|
||||
if (fread(buffer, fsize, 1, f) != 1)
|
||||
return NULL;
|
||||
fclose(f);
|
||||
|
||||
JA_Music_t *music = JA_LoadMusic(buffer, fsize);
|
||||
|
||||
free(buffer);
|
||||
|
||||
return music;
|
||||
}
|
||||
|
||||
void JA_PlayMusic(JA_Music_t *music, const int loop)
|
||||
{
|
||||
if (!JA_musicEnabled)
|
||||
return;
|
||||
|
||||
if (current_music != NULL)
|
||||
{
|
||||
current_music->pos = 0;
|
||||
current_music->state = JA_MUSIC_STOPPED;
|
||||
}
|
||||
current_music = music;
|
||||
current_music->pos = 0;
|
||||
current_music->state = JA_MUSIC_PLAYING;
|
||||
current_music->times = loop;
|
||||
}
|
||||
|
||||
void JA_PauseMusic()
|
||||
{
|
||||
if (!JA_musicEnabled)
|
||||
return;
|
||||
|
||||
if (current_music == NULL || current_music->state == JA_MUSIC_INVALID)
|
||||
return;
|
||||
current_music->state = JA_MUSIC_PAUSED;
|
||||
}
|
||||
|
||||
void JA_ResumeMusic()
|
||||
{
|
||||
if (!JA_musicEnabled)
|
||||
return;
|
||||
|
||||
if (current_music == NULL || current_music->state == JA_MUSIC_INVALID)
|
||||
return;
|
||||
current_music->state = JA_MUSIC_PLAYING;
|
||||
}
|
||||
|
||||
void JA_StopMusic()
|
||||
{
|
||||
if (!JA_musicEnabled)
|
||||
return;
|
||||
|
||||
if (current_music == NULL || current_music->state == JA_MUSIC_INVALID)
|
||||
return;
|
||||
current_music->pos = 0;
|
||||
current_music->state = JA_MUSIC_STOPPED;
|
||||
}
|
||||
|
||||
void JA_FadeOutMusic(const int milliseconds)
|
||||
{
|
||||
if (!JA_musicEnabled)
|
||||
return;
|
||||
if (current_music == NULL || current_music->state == JA_MUSIC_INVALID)
|
||||
return;
|
||||
|
||||
fading = true;
|
||||
fade_start_time = SDL_GetTicks();
|
||||
fade_duration = milliseconds;
|
||||
fade_initial_volume = JA_musicVolume;
|
||||
}
|
||||
|
||||
JA_Music_state JA_GetMusicState()
|
||||
{
|
||||
if (!JA_musicEnabled)
|
||||
return JA_MUSIC_DISABLED;
|
||||
|
||||
if (current_music == NULL)
|
||||
return JA_MUSIC_INVALID;
|
||||
return current_music->state;
|
||||
}
|
||||
|
||||
void JA_DeleteMusic(JA_Music_t *music)
|
||||
{
|
||||
if (current_music == music)
|
||||
current_music = NULL;
|
||||
free(music->output);
|
||||
delete music;
|
||||
}
|
||||
|
||||
int JA_SetMusicVolume(int volume)
|
||||
{
|
||||
JA_musicVolume = volume > 128 ? 128 : volume < 0 ? 0
|
||||
: volume;
|
||||
return JA_musicVolume;
|
||||
}
|
||||
|
||||
void JA_SetMusicPosition(float value)
|
||||
{
|
||||
if (!current_music)
|
||||
return;
|
||||
current_music->pos = value * JA_freq;
|
||||
}
|
||||
|
||||
float JA_GetMusicPosition()
|
||||
{
|
||||
if (!current_music)
|
||||
return 0;
|
||||
return float(current_music->pos) / float(JA_freq);
|
||||
}
|
||||
|
||||
void JA_EnableMusic(const bool value)
|
||||
{
|
||||
if (!value && current_music != NULL && current_music->state == JA_MUSIC_PLAYING)
|
||||
JA_StopMusic();
|
||||
|
||||
JA_musicEnabled = value;
|
||||
}
|
||||
|
||||
JA_Sound_t *JA_NewSound(Uint8 *buffer, Uint32 length)
|
||||
{
|
||||
JA_Sound_t *sound = new JA_Sound_t();
|
||||
sound->buffer = buffer;
|
||||
sound->length = length;
|
||||
return sound;
|
||||
}
|
||||
|
||||
JA_Sound_t *JA_LoadSound(uint8_t *buffer, uint32_t size)
|
||||
{
|
||||
JA_Sound_t *sound = new JA_Sound_t();
|
||||
SDL_AudioSpec wavSpec;
|
||||
SDL_LoadWAV_RW(SDL_RWFromMem(buffer, size), 1, &wavSpec, &sound->buffer, &sound->length);
|
||||
|
||||
SDL_AudioCVT cvt;
|
||||
SDL_BuildAudioCVT(&cvt, wavSpec.format, wavSpec.channels, wavSpec.freq, JA_format, JA_channels, JA_freq);
|
||||
cvt.len = sound->length;
|
||||
cvt.buf = (Uint8 *)SDL_malloc(cvt.len * cvt.len_mult);
|
||||
SDL_memcpy(cvt.buf, sound->buffer, sound->length);
|
||||
SDL_ConvertAudio(&cvt);
|
||||
SDL_FreeWAV(sound->buffer);
|
||||
sound->buffer = cvt.buf;
|
||||
sound->length = cvt.len_cvt;
|
||||
|
||||
return sound;
|
||||
}
|
||||
|
||||
JA_Sound_t *JA_LoadSound(const char *filename)
|
||||
{
|
||||
JA_Sound_t *sound = new JA_Sound_t();
|
||||
SDL_AudioSpec wavSpec;
|
||||
SDL_LoadWAV(filename, &wavSpec, &sound->buffer, &sound->length);
|
||||
|
||||
SDL_AudioCVT cvt;
|
||||
SDL_BuildAudioCVT(&cvt, wavSpec.format, wavSpec.channels, wavSpec.freq, JA_format, JA_channels, JA_freq);
|
||||
cvt.len = sound->length;
|
||||
cvt.buf = (Uint8 *)SDL_malloc(cvt.len * cvt.len_mult);
|
||||
SDL_memcpy(cvt.buf, sound->buffer, sound->length);
|
||||
SDL_ConvertAudio(&cvt);
|
||||
SDL_FreeWAV(sound->buffer);
|
||||
sound->buffer = cvt.buf;
|
||||
sound->length = cvt.len_cvt;
|
||||
|
||||
return sound;
|
||||
}
|
||||
|
||||
int JA_PlaySound(JA_Sound_t *sound, const int loop)
|
||||
{
|
||||
if (!JA_soundEnabled)
|
||||
return -1;
|
||||
|
||||
int channel = 0;
|
||||
while (channel < JA_MAX_SIMULTANEOUS_CHANNELS && channels[channel].state != JA_CHANNEL_FREE)
|
||||
{
|
||||
channel++;
|
||||
}
|
||||
if (channel == JA_MAX_SIMULTANEOUS_CHANNELS)
|
||||
channel = 0;
|
||||
|
||||
channels[channel].sound = sound;
|
||||
channels[channel].times = loop;
|
||||
channels[channel].pos = 0;
|
||||
channels[channel].state = JA_CHANNEL_PLAYING;
|
||||
return channel;
|
||||
}
|
||||
|
||||
int JA_PlaySoundOnChannel(JA_Sound_t *sound, const int channel, const int loop)
|
||||
{
|
||||
if (!JA_soundEnabled)
|
||||
return -1;
|
||||
|
||||
if (channel >= JA_MAX_SIMULTANEOUS_CHANNELS)
|
||||
return -1;
|
||||
|
||||
channels[channel].sound = sound;
|
||||
channels[channel].times = loop;
|
||||
channels[channel].pos = 0;
|
||||
channels[channel].state = JA_CHANNEL_PLAYING;
|
||||
return channel;
|
||||
}
|
||||
|
||||
void JA_DeleteSound(JA_Sound_t *sound)
|
||||
{
|
||||
for (int i = 0; i < JA_MAX_SIMULTANEOUS_CHANNELS; i++)
|
||||
{
|
||||
if (channels[i].sound == sound)
|
||||
JA_StopChannel(i);
|
||||
}
|
||||
SDL_free(sound->buffer);
|
||||
delete sound;
|
||||
}
|
||||
|
||||
void JA_PauseChannel(const int channel)
|
||||
{
|
||||
if (!JA_soundEnabled)
|
||||
return;
|
||||
|
||||
if (channel == -1)
|
||||
{
|
||||
for (int i = 0; i < JA_MAX_SIMULTANEOUS_CHANNELS; i++)
|
||||
{
|
||||
if (channels[i].state == JA_CHANNEL_PLAYING)
|
||||
channels[i].state = JA_CHANNEL_PAUSED;
|
||||
}
|
||||
}
|
||||
else if (channel >= 0 && channel < JA_MAX_SIMULTANEOUS_CHANNELS)
|
||||
{
|
||||
if (channels[channel].state == JA_CHANNEL_PLAYING)
|
||||
channels[channel].state = JA_CHANNEL_PAUSED;
|
||||
}
|
||||
}
|
||||
|
||||
void JA_ResumeChannel(const int channel)
|
||||
{
|
||||
if (!JA_soundEnabled)
|
||||
return;
|
||||
|
||||
if (channel == -1)
|
||||
{
|
||||
for (int i = 0; i < JA_MAX_SIMULTANEOUS_CHANNELS; i++)
|
||||
{
|
||||
if (channels[i].state == JA_CHANNEL_PAUSED)
|
||||
channels[i].state = JA_CHANNEL_PLAYING;
|
||||
}
|
||||
}
|
||||
else if (channel >= 0 && channel < JA_MAX_SIMULTANEOUS_CHANNELS)
|
||||
{
|
||||
if (channels[channel].state == JA_CHANNEL_PAUSED)
|
||||
channels[channel].state = JA_CHANNEL_PLAYING;
|
||||
}
|
||||
}
|
||||
|
||||
void JA_StopChannel(const int channel)
|
||||
{
|
||||
if (!JA_soundEnabled)
|
||||
return;
|
||||
|
||||
if (channel == -1)
|
||||
{
|
||||
for (int i = 0; i < JA_MAX_SIMULTANEOUS_CHANNELS; i++)
|
||||
{
|
||||
channels[i].state = JA_CHANNEL_FREE;
|
||||
channels[i].pos = 0;
|
||||
channels[i].sound = NULL;
|
||||
}
|
||||
}
|
||||
else if (channel >= 0 && channel < JA_MAX_SIMULTANEOUS_CHANNELS)
|
||||
{
|
||||
channels[channel].state = JA_CHANNEL_FREE;
|
||||
channels[channel].pos = 0;
|
||||
channels[channel].sound = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
JA_Channel_state JA_GetChannelState(const int channel)
|
||||
{
|
||||
if (!JA_soundEnabled)
|
||||
return JA_SOUND_DISABLED;
|
||||
|
||||
if (channel < 0 || channel >= JA_MAX_SIMULTANEOUS_CHANNELS)
|
||||
return JA_CHANNEL_INVALID;
|
||||
return channels[channel].state;
|
||||
}
|
||||
|
||||
int JA_SetSoundVolume(int volume)
|
||||
{
|
||||
JA_soundVolume = volume > 128 ? 128 : volume < 0 ? 0
|
||||
: volume;
|
||||
return JA_soundVolume;
|
||||
}
|
||||
|
||||
void JA_EnableSound(const bool value)
|
||||
{
|
||||
for (int i = 0; i < JA_MAX_SIMULTANEOUS_CHANNELS; i++)
|
||||
{
|
||||
if (channels[i].state == JA_CHANNEL_PLAYING)
|
||||
JA_StopChannel(i);
|
||||
}
|
||||
JA_soundEnabled = value;
|
||||
}
|
||||
|
||||
int JA_SetVolume(int volume)
|
||||
{
|
||||
JA_musicVolume = volume > 128 ? 128 : volume < 0 ? 0
|
||||
: volume;
|
||||
JA_soundVolume = JA_musicVolume / 2;
|
||||
return JA_musicVolume;
|
||||
}
|
||||
58
source/jail_audio.h
Normal file
58
source/jail_audio.h
Normal file
@@ -0,0 +1,58 @@
|
||||
#pragma once
|
||||
|
||||
#include <SDL2/SDL_audio.h> // for SDL_AudioFormat
|
||||
#include <SDL2/SDL_stdinc.h> // for Uint32, Uint8
|
||||
struct JA_Music_t; // lines 8-8
|
||||
struct JA_Sound_t; // lines 7-7
|
||||
|
||||
enum JA_Channel_state
|
||||
{
|
||||
JA_CHANNEL_INVALID,
|
||||
JA_CHANNEL_FREE,
|
||||
JA_CHANNEL_PLAYING,
|
||||
JA_CHANNEL_PAUSED,
|
||||
JA_SOUND_DISABLED
|
||||
};
|
||||
enum JA_Music_state
|
||||
{
|
||||
JA_MUSIC_INVALID,
|
||||
JA_MUSIC_PLAYING,
|
||||
JA_MUSIC_PAUSED,
|
||||
JA_MUSIC_STOPPED,
|
||||
JA_MUSIC_DISABLED
|
||||
};
|
||||
|
||||
struct JA_Sound_t;
|
||||
struct JA_Music_t;
|
||||
|
||||
void JA_Init(const int freq, const SDL_AudioFormat format, const int channels);
|
||||
void JA_Quit();
|
||||
|
||||
JA_Music_t *JA_LoadMusic(const char *filename);
|
||||
JA_Music_t *JA_LoadMusic(Uint8 *buffer, Uint32 length);
|
||||
void JA_PlayMusic(JA_Music_t *music, const int loop = -1);
|
||||
void JA_PauseMusic();
|
||||
void JA_ResumeMusic();
|
||||
void JA_StopMusic();
|
||||
void JA_FadeOutMusic(const int milliseconds);
|
||||
JA_Music_state JA_GetMusicState();
|
||||
void JA_DeleteMusic(JA_Music_t *music);
|
||||
int JA_SetMusicVolume(int volume);
|
||||
void JA_SetMusicPosition(float value);
|
||||
float JA_GetMusicPosition();
|
||||
void JA_EnableMusic(const bool value);
|
||||
|
||||
JA_Sound_t *JA_NewSound(Uint8 *buffer, Uint32 length);
|
||||
JA_Sound_t *JA_LoadSound(Uint8 *buffer, Uint32 length);
|
||||
JA_Sound_t *JA_LoadSound(const char *filename);
|
||||
int JA_PlaySound(JA_Sound_t *sound, const int loop = 0);
|
||||
int JA_PlaySoundOnChannel(JA_Sound_t *sound, const int channel, const int loop = 0);
|
||||
void JA_PauseChannel(const int channel);
|
||||
void JA_ResumeChannel(const int channel);
|
||||
void JA_StopChannel(const int channel);
|
||||
JA_Channel_state JA_GetChannelState(const int channel);
|
||||
void JA_DeleteSound(JA_Sound_t *sound);
|
||||
int JA_SetSoundVolume(int volume);
|
||||
void JA_EnableSound(const bool value);
|
||||
|
||||
int JA_SetVolume(int volume);
|
||||
@@ -1,106 +0,0 @@
|
||||
#include "debug.h"
|
||||
|
||||
// Constructor
|
||||
Debug::Debug(SDL_Renderer *renderer, Screen *screen, Asset *asset)
|
||||
{
|
||||
// Copia la dirección de los objetos
|
||||
this->renderer = renderer;
|
||||
this->screen = screen;
|
||||
this->asset = asset;
|
||||
|
||||
// Reserva memoria para los punteros
|
||||
texture = new Texture(renderer, asset->get("debug.png"));
|
||||
text = new Text(asset->get("debug.txt"), texture, renderer);
|
||||
|
||||
// Inicializa variables
|
||||
x = 0;
|
||||
y = 0;
|
||||
enabled = false;
|
||||
}
|
||||
|
||||
// Destructor
|
||||
Debug::~Debug()
|
||||
{
|
||||
delete texture;
|
||||
delete text;
|
||||
}
|
||||
|
||||
// Actualiza las variables
|
||||
void Debug::update()
|
||||
{
|
||||
}
|
||||
|
||||
// Dibuja en pantalla
|
||||
void Debug::render()
|
||||
{
|
||||
int y = this->y;
|
||||
int w = 0;
|
||||
|
||||
for (auto s : slot)
|
||||
{
|
||||
text->write(x, y, s);
|
||||
w = (std::max(w, (int)s.length()));
|
||||
y += text->getCharacterSize() + 1;
|
||||
if (y > 192 - text->getCharacterSize())
|
||||
{
|
||||
y = this->y;
|
||||
x += w * text->getCharacterSize() + 2;
|
||||
}
|
||||
}
|
||||
|
||||
y = 0;
|
||||
for (auto l : log)
|
||||
{
|
||||
text->writeColored(x + 10, y, l, {255, 255, 255});
|
||||
y += text->getCharacterSize() + 1;
|
||||
}
|
||||
}
|
||||
|
||||
// Establece la posición donde se colocará la información de debug
|
||||
void Debug::setPos(SDL_Point p)
|
||||
{
|
||||
x = p.x;
|
||||
y = p.y;
|
||||
}
|
||||
|
||||
// Añade un texto para mostrar
|
||||
void Debug::add(std::string text)
|
||||
{
|
||||
slot.push_back(text);
|
||||
}
|
||||
|
||||
// Borra la información de debug
|
||||
void Debug::clear()
|
||||
{
|
||||
slot.clear();
|
||||
}
|
||||
|
||||
// Añade un texto para mostrar en el apartado log
|
||||
void Debug::addToLog(std::string text)
|
||||
{
|
||||
log.push_back(text);
|
||||
}
|
||||
|
||||
// Borra la información de debug del apartado log
|
||||
void Debug::clearLog()
|
||||
{
|
||||
log.clear();
|
||||
}
|
||||
|
||||
// Establece el valor de la variable
|
||||
void Debug::setEnabled(bool value)
|
||||
{
|
||||
enabled = value;
|
||||
}
|
||||
|
||||
// Obtiene el valor de la variable
|
||||
bool Debug::getEnabled()
|
||||
{
|
||||
return enabled;
|
||||
}
|
||||
|
||||
// Cambia el valor de la variable
|
||||
void Debug::switchEnabled()
|
||||
{
|
||||
enabled = !enabled;
|
||||
}
|
||||
@@ -1,72 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include <SDL2/SDL.h>
|
||||
#include "../const.h"
|
||||
#include "asset.h"
|
||||
#include "screen.h"
|
||||
#include "text.h"
|
||||
#include "texture.h"
|
||||
#include "utils.h"
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#ifndef DEBUG_H
|
||||
#define DEBUG_H
|
||||
|
||||
// Clase Debug
|
||||
class Debug
|
||||
{
|
||||
private:
|
||||
// Objetos y punteros
|
||||
SDL_Renderer *renderer; // El renderizador de la ventana
|
||||
Screen *screen; // Objeto encargado de dibujar en pantalla
|
||||
Asset *asset; // Objeto con los ficheros de recursos
|
||||
Text *text; // Objeto encargado de escribir texto en pantalla
|
||||
Texture *texture; // Textura para el texto
|
||||
|
||||
// Variables
|
||||
std::vector<std::string> slot; // Vector con los textos a escribir
|
||||
std::vector<std::string> log; // Vector con los textos a escribir
|
||||
int x; // Posicion donde escribir el texto de debug
|
||||
int y; // Posición donde escribir el texto de debug
|
||||
bool enabled; // Indica si esta activo el modo debug
|
||||
|
||||
public:
|
||||
// Constructor
|
||||
Debug(SDL_Renderer *renderer, Screen *screen, Asset *asset);
|
||||
|
||||
// Destructor
|
||||
~Debug();
|
||||
|
||||
// Actualiza las variables
|
||||
void update();
|
||||
|
||||
// Dibuja en pantalla
|
||||
void render();
|
||||
|
||||
// Establece la posición donde se colocará la información de debug
|
||||
void setPos(SDL_Point p);
|
||||
|
||||
// Añade un texto para mostrar
|
||||
void add(std::string text);
|
||||
|
||||
// Borra la información de debug
|
||||
void clear();
|
||||
|
||||
// Añade un texto para mostrar en el apartado log
|
||||
void addToLog(std::string text);
|
||||
|
||||
// Borra la información de debug del apartado log
|
||||
void clearLog();
|
||||
|
||||
// Establece el valor de la variable
|
||||
void setEnabled(bool value);
|
||||
|
||||
// Obtiene el valor de la variable
|
||||
bool getEnabled();
|
||||
|
||||
// Cambia el valor de la variable
|
||||
void switchEnabled();
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -1,119 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include <SDL2/SDL.h>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#ifndef INPUT_H
|
||||
#define INPUT_H
|
||||
|
||||
enum inputs_e
|
||||
{
|
||||
// Inputs obligatorios
|
||||
input_null,
|
||||
input_up,
|
||||
input_down,
|
||||
input_left,
|
||||
input_right,
|
||||
input_pause,
|
||||
input_exit,
|
||||
input_accept,
|
||||
input_cancel,
|
||||
|
||||
// Inputs personalizados
|
||||
input_jump,
|
||||
input_window_fullscreen,
|
||||
input_window_inc_size,
|
||||
input_window_dec_size,
|
||||
input_toggle_border,
|
||||
input_switch_music,
|
||||
input_swap_palette,
|
||||
|
||||
// Input obligatorio
|
||||
input_number_of_inputs
|
||||
};
|
||||
|
||||
#define REPEAT_TRUE true
|
||||
#define REPEAT_FALSE false
|
||||
|
||||
#define INPUT_USE_KEYBOARD 0
|
||||
#define INPUT_USE_GAMECONTROLLER 1
|
||||
#define INPUT_USE_ANY 2
|
||||
|
||||
enum i_disable_e
|
||||
{
|
||||
d_notDisabled,
|
||||
d_forever,
|
||||
d_keyPressed
|
||||
};
|
||||
|
||||
class Input
|
||||
{
|
||||
private:
|
||||
struct keyBindings_t
|
||||
{
|
||||
Uint8 scancode; // Scancode asociado
|
||||
bool active; // Indica si está activo
|
||||
};
|
||||
|
||||
struct GameControllerBindings_t
|
||||
{
|
||||
SDL_GameControllerButton button; // GameControllerButton asociado
|
||||
bool active; // Indica si está activo
|
||||
};
|
||||
|
||||
// Objetos y punteros
|
||||
std::vector<SDL_GameController *> connectedControllers; // Vector con todos los mandos conectados
|
||||
|
||||
// Variables
|
||||
std::vector<keyBindings_t> keyBindings; // Vector con las teclas asociadas a los inputs predefinidos
|
||||
std::vector<GameControllerBindings_t> gameControllerBindings; // Vector con las teclas asociadas a los inputs predefinidos
|
||||
std::vector<std::string> controllerNames; // Vector con los nombres de los mandos
|
||||
int numGamepads; // Numero de mandos conectados
|
||||
std::string dbPath; // Ruta al archivo gamecontrollerdb.txt
|
||||
bool verbose; // Indica si ha de mostrar mensajes
|
||||
i_disable_e disabledUntil; // Tiempo que esta deshabilitado
|
||||
bool enabled; // Indica si está habilitado
|
||||
|
||||
public:
|
||||
// Constructor
|
||||
Input(std::string file);
|
||||
|
||||
// Actualiza el estado del objeto
|
||||
void update();
|
||||
|
||||
// Asigna inputs a teclas
|
||||
void bindKey(Uint8 input, SDL_Scancode code);
|
||||
|
||||
// Asigna inputs a botones del mando
|
||||
void bindGameControllerButton(Uint8 input, SDL_GameControllerButton button);
|
||||
|
||||
// Comprueba si un input esta activo
|
||||
bool checkInput(Uint8 input, bool repeat = true, int device = INPUT_USE_ANY, int index = 0);
|
||||
|
||||
// Comprueba si hay almenos un input activo
|
||||
bool checkAnyInput(int device = INPUT_USE_ANY, int index = 0);
|
||||
|
||||
// Busca si hay un mando conectado
|
||||
bool discoverGameController();
|
||||
|
||||
// Comprueba si hay algun mando conectado
|
||||
bool gameControllerFound();
|
||||
|
||||
// Obten el numero de mandos conectados
|
||||
int getNumControllers();
|
||||
|
||||
// Obten el nombre de un mando de juego
|
||||
std::string getControllerName(int index);
|
||||
|
||||
// Establece si ha de mostrar mensajes
|
||||
void setVerbose(bool value);
|
||||
|
||||
// Deshabilita las entradas durante un periodo de tiempo
|
||||
void disableUntil(i_disable_e value);
|
||||
|
||||
// Hablita las entradas
|
||||
void enable();
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -1,251 +0,0 @@
|
||||
#ifndef JA_USESDLMIXER
|
||||
#include "jail_audio.h"
|
||||
#include "stb_vorbis.c"
|
||||
#include <SDL2/SDL.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#define JA_MAX_SIMULTANEOUS_CHANNELS 5
|
||||
|
||||
struct JA_Sound_t {
|
||||
Uint32 length {0};
|
||||
Uint8* buffer {NULL};
|
||||
};
|
||||
|
||||
struct JA_Channel_t {
|
||||
JA_Sound_t *sound;
|
||||
int pos {0};
|
||||
int times {0};
|
||||
JA_Channel_state state { JA_CHANNEL_FREE };
|
||||
};
|
||||
|
||||
struct JA_Music_t {
|
||||
int samples {0};
|
||||
int pos {0};
|
||||
int times {0};
|
||||
short* output {NULL};
|
||||
JA_Music_state state {JA_MUSIC_INVALID};
|
||||
};
|
||||
|
||||
JA_Music_t *current_music{NULL};
|
||||
JA_Channel_t channels[JA_MAX_SIMULTANEOUS_CHANNELS];
|
||||
|
||||
int JA_freq {48000};
|
||||
SDL_AudioFormat JA_format {AUDIO_S16};
|
||||
Uint8 JA_channels {2};
|
||||
int JA_volume = 128;
|
||||
SDL_AudioDeviceID sdlAudioDevice = 0;
|
||||
|
||||
void audioCallback(void * userdata, uint8_t * stream, int len) {
|
||||
SDL_memset(stream, 0, len);
|
||||
if (current_music != NULL && current_music->state == JA_MUSIC_PLAYING) {
|
||||
const int size = SDL_min(len, current_music->samples*2-current_music->pos);
|
||||
SDL_MixAudioFormat(stream, (Uint8*)(current_music->output+current_music->pos), AUDIO_S16, size, JA_volume);
|
||||
current_music->pos += size/2;
|
||||
if (size < len) {
|
||||
if (current_music->times != 0) {
|
||||
SDL_MixAudioFormat(stream+size, (Uint8*)current_music->output, AUDIO_S16, len-size, JA_volume);
|
||||
current_music->pos = (len-size)/2;
|
||||
if (current_music->times > 0) current_music->times--;
|
||||
} else {
|
||||
current_music->pos = 0;
|
||||
current_music->state = JA_MUSIC_STOPPED;
|
||||
}
|
||||
}
|
||||
}
|
||||
// Mixar els channels mi amol
|
||||
for (int i = 0; i < JA_MAX_SIMULTANEOUS_CHANNELS; i++) {
|
||||
if (channels[i].state == JA_CHANNEL_PLAYING) {
|
||||
const int size = SDL_min(len, channels[i].sound->length - channels[i].pos);
|
||||
SDL_MixAudioFormat(stream, channels[i].sound->buffer + channels[i].pos, AUDIO_S16, size, JA_volume/2);
|
||||
channels[i].pos += size;
|
||||
if (size < len) {
|
||||
if (channels[i].times != 0) {
|
||||
SDL_MixAudioFormat(stream + size, channels[i].sound->buffer, AUDIO_S16, len-size, JA_volume/2);
|
||||
channels[i].pos = len-size;
|
||||
if (channels[i].times > 0) channels[i].times--;
|
||||
} else {
|
||||
JA_StopChannel(i);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void JA_Init(const int freq, const SDL_AudioFormat format, const int channels) {
|
||||
JA_freq = freq;
|
||||
JA_format = format;
|
||||
JA_channels = channels;
|
||||
SDL_AudioSpec audioSpec{JA_freq, JA_format, JA_channels, 0, 1024, 0, 0, audioCallback, NULL};
|
||||
if (sdlAudioDevice != 0) SDL_CloseAudioDevice(sdlAudioDevice);
|
||||
sdlAudioDevice = SDL_OpenAudioDevice(NULL, 0, &audioSpec, NULL, 0);
|
||||
SDL_PauseAudioDevice(sdlAudioDevice, 0);
|
||||
}
|
||||
|
||||
void JA_Quit() {
|
||||
SDL_PauseAudioDevice(sdlAudioDevice, 1);
|
||||
if (sdlAudioDevice != 0) SDL_CloseAudioDevice(sdlAudioDevice);
|
||||
sdlAudioDevice = 0;
|
||||
}
|
||||
|
||||
JA_Music_t *JA_LoadMusic(const char* filename) {
|
||||
int chan, samplerate;
|
||||
|
||||
// [RZC 28/08/22] Carreguem primer el arxiu en memòria i després el descomprimim. Es algo més rapid.
|
||||
FILE *f = fopen(filename, "rb");
|
||||
fseek(f, 0, SEEK_END);
|
||||
long fsize = ftell(f);
|
||||
fseek(f, 0, SEEK_SET);
|
||||
Uint8 *buffer = (Uint8*)malloc(fsize + 1);
|
||||
if (fread(buffer, fsize, 1, f)!=1) return NULL;
|
||||
fclose(f);
|
||||
|
||||
JA_Music_t *music = new JA_Music_t();
|
||||
|
||||
music->samples = stb_vorbis_decode_memory(buffer, fsize, &chan, &samplerate, &music->output);
|
||||
free(buffer);
|
||||
// [RZC 28/08/22] Abans el descomprimiem mentre el teniem obert
|
||||
// music->samples = stb_vorbis_decode_filename(filename, &chan, &samplerate, &music->output);
|
||||
|
||||
SDL_AudioCVT cvt;
|
||||
SDL_BuildAudioCVT(&cvt, AUDIO_S16, chan, samplerate, JA_format, JA_channels, JA_freq);
|
||||
if (cvt.needed) {
|
||||
cvt.len = music->samples * chan * 2;
|
||||
cvt.buf = (Uint8 *) SDL_malloc(cvt.len * cvt.len_mult);
|
||||
SDL_memcpy(cvt.buf, music->output, cvt.len);
|
||||
SDL_ConvertAudio(&cvt);
|
||||
free(music->output);
|
||||
music->output = (short*)cvt.buf;
|
||||
}
|
||||
music->pos = 0;
|
||||
music->state = JA_MUSIC_STOPPED;
|
||||
|
||||
return music;
|
||||
}
|
||||
|
||||
void JA_PlayMusic(JA_Music_t *music, const int loop) {
|
||||
if (current_music != NULL) {
|
||||
current_music->pos = 0;
|
||||
current_music->state = JA_MUSIC_STOPPED;
|
||||
}
|
||||
current_music = music;
|
||||
current_music->pos = 0;
|
||||
current_music->state = JA_MUSIC_PLAYING;
|
||||
current_music->times = loop;
|
||||
}
|
||||
|
||||
void JA_PauseMusic() {
|
||||
if (current_music == NULL || current_music->state == JA_MUSIC_INVALID) return;
|
||||
current_music->state = JA_MUSIC_PAUSED;
|
||||
}
|
||||
|
||||
void JA_ResumeMusic() {
|
||||
if (current_music == NULL || current_music->state == JA_MUSIC_INVALID) return;
|
||||
current_music->state = JA_MUSIC_PLAYING;
|
||||
}
|
||||
|
||||
void JA_StopMusic() {
|
||||
if (current_music == NULL || current_music->state == JA_MUSIC_INVALID) return;
|
||||
current_music->pos = 0;
|
||||
current_music->state = JA_MUSIC_STOPPED;
|
||||
}
|
||||
|
||||
JA_Music_state JA_GetMusicState() {
|
||||
if (current_music == NULL) return JA_MUSIC_INVALID;
|
||||
return current_music->state;
|
||||
}
|
||||
|
||||
void JA_DeleteMusic(JA_Music_t *music) {
|
||||
if (current_music == music) current_music = NULL;
|
||||
free(music->output);
|
||||
delete music;
|
||||
}
|
||||
|
||||
JA_Sound_t *JA_NewSound(Uint8* buffer, Uint32 length) {
|
||||
JA_Sound_t *sound = new JA_Sound_t();
|
||||
sound->buffer = buffer;
|
||||
sound->length = length;
|
||||
return sound;
|
||||
}
|
||||
|
||||
JA_Sound_t *JA_LoadSound(const char* filename) {
|
||||
JA_Sound_t *sound = new JA_Sound_t();
|
||||
SDL_AudioSpec wavSpec;
|
||||
SDL_LoadWAV(filename, &wavSpec, &sound->buffer, &sound->length);
|
||||
|
||||
SDL_AudioCVT cvt;
|
||||
SDL_BuildAudioCVT(&cvt, wavSpec.format, wavSpec.channels, wavSpec.freq, JA_format, JA_channels, JA_freq);
|
||||
cvt.len = sound->length;
|
||||
cvt.buf = (Uint8 *) SDL_malloc(cvt.len * cvt.len_mult);
|
||||
SDL_memcpy(cvt.buf, sound->buffer, sound->length);
|
||||
SDL_ConvertAudio(&cvt);
|
||||
SDL_FreeWAV(sound->buffer);
|
||||
sound->buffer = cvt.buf;
|
||||
sound->length = cvt.len_cvt;
|
||||
|
||||
return sound;
|
||||
}
|
||||
|
||||
int JA_PlaySound(JA_Sound_t *sound, const int loop) {
|
||||
int channel = 0;
|
||||
while (channel < JA_MAX_SIMULTANEOUS_CHANNELS && channels[channel].state != JA_CHANNEL_FREE) { channel++; }
|
||||
if (channel == JA_MAX_SIMULTANEOUS_CHANNELS) channel = 0;
|
||||
|
||||
channels[channel].sound = sound;
|
||||
channels[channel].times = loop;
|
||||
channels[channel].pos = 0;
|
||||
channels[channel].state = JA_CHANNEL_PLAYING;
|
||||
return channel;
|
||||
}
|
||||
|
||||
void JA_DeleteSound(JA_Sound_t *sound) {
|
||||
for (int i = 0; i < JA_MAX_SIMULTANEOUS_CHANNELS; i++) {
|
||||
if (channels[i].sound == sound) JA_StopChannel(i);
|
||||
}
|
||||
SDL_free(sound->buffer);
|
||||
delete sound;
|
||||
}
|
||||
|
||||
void JA_PauseChannel(const int channel) {
|
||||
if (channel == -1) {
|
||||
for (int i = 0; i < JA_MAX_SIMULTANEOUS_CHANNELS; i++) {
|
||||
if (channels[i].state == JA_CHANNEL_PLAYING) channels[i].state = JA_CHANNEL_PAUSED;
|
||||
}
|
||||
} else if (channel >= 0 && channel < JA_MAX_SIMULTANEOUS_CHANNELS) {
|
||||
if (channels[channel].state == JA_CHANNEL_PLAYING) channels[channel].state = JA_CHANNEL_PAUSED;
|
||||
}
|
||||
}
|
||||
|
||||
void JA_ResumeChannel(const int channel) {
|
||||
if (channel == -1) {
|
||||
for (int i = 0; i < JA_MAX_SIMULTANEOUS_CHANNELS; i++) {
|
||||
if (channels[i].state == JA_CHANNEL_PAUSED) channels[i].state = JA_CHANNEL_PLAYING;
|
||||
}
|
||||
} else if (channel >= 0 && channel < JA_MAX_SIMULTANEOUS_CHANNELS) {
|
||||
if (channels[channel].state == JA_CHANNEL_PAUSED) channels[channel].state = JA_CHANNEL_PLAYING;
|
||||
}
|
||||
}
|
||||
|
||||
void JA_StopChannel(const int channel) {
|
||||
if (channel == -1) {
|
||||
for (int i = 0; i < JA_MAX_SIMULTANEOUS_CHANNELS; i++) {
|
||||
channels[i].state = JA_CHANNEL_FREE;
|
||||
channels[i].pos = 0;
|
||||
channels[i].sound = NULL;
|
||||
}
|
||||
} else if (channel >= 0 && channel < JA_MAX_SIMULTANEOUS_CHANNELS) {
|
||||
channels[channel].state = JA_CHANNEL_FREE;
|
||||
channels[channel].pos = 0;
|
||||
channels[channel].sound = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
JA_Channel_state JA_GetChannelState(const int channel) {
|
||||
if (channel < 0 || channel >= JA_MAX_SIMULTANEOUS_CHANNELS) return JA_CHANNEL_INVALID;
|
||||
return channels[channel].state;
|
||||
}
|
||||
|
||||
int JA_SetVolume(int volume) {
|
||||
JA_volume = volume > 128 ? 128 : volume < 0 ? 0 : volume;
|
||||
return JA_volume;
|
||||
}
|
||||
#endif
|
||||
@@ -1,30 +0,0 @@
|
||||
#pragma once
|
||||
#include <SDL2/SDL.h>
|
||||
|
||||
enum JA_Channel_state { JA_CHANNEL_INVALID, JA_CHANNEL_FREE, JA_CHANNEL_PLAYING, JA_CHANNEL_PAUSED };
|
||||
enum JA_Music_state { JA_MUSIC_INVALID, JA_MUSIC_PLAYING, JA_MUSIC_PAUSED, JA_MUSIC_STOPPED };
|
||||
|
||||
struct JA_Sound_t;
|
||||
struct JA_Music_t;
|
||||
|
||||
void JA_Init(const int freq, const SDL_AudioFormat format, const int channels);
|
||||
void JA_Quit();
|
||||
|
||||
JA_Music_t *JA_LoadMusic(const char* filename);
|
||||
void JA_PlayMusic(JA_Music_t *music, const int loop = -1);
|
||||
void JA_PauseMusic();
|
||||
void JA_ResumeMusic();
|
||||
void JA_StopMusic();
|
||||
JA_Music_state JA_GetMusicState();
|
||||
void JA_DeleteMusic(JA_Music_t *music);
|
||||
|
||||
JA_Sound_t *JA_NewSound(Uint8* buffer, Uint32 length);
|
||||
JA_Sound_t *JA_LoadSound(const char* filename);
|
||||
int JA_PlaySound(JA_Sound_t *sound, const int loop = 0);
|
||||
void JA_PauseChannel(const int channel);
|
||||
void JA_ResumeChannel(const int channel);
|
||||
void JA_StopChannel(const int channel);
|
||||
JA_Channel_state JA_GetChannelState(const int channel);
|
||||
void JA_DeleteSound(JA_Sound_t *sound);
|
||||
|
||||
int JA_SetVolume(int volume);
|
||||
@@ -1,101 +0,0 @@
|
||||
#ifdef JA_USESDLMIXER
|
||||
#include "jail_audio.h"
|
||||
#include <SDL2/SDL.h>
|
||||
#include <SDL2/SDL_mixer.h>
|
||||
#include <stdio.h>
|
||||
|
||||
struct JA_Sound_t {}; // Dummy structs
|
||||
struct JA_Music_t {};
|
||||
|
||||
int JA_freq {48000};
|
||||
SDL_AudioFormat JA_format {AUDIO_S16};
|
||||
Uint8 JA_channels {2};
|
||||
|
||||
void JA_Init(const int freq, const SDL_AudioFormat format, const int channels) {
|
||||
JA_freq = freq;
|
||||
JA_format = format;
|
||||
JA_channels = channels;
|
||||
Mix_OpenAudio(JA_freq, JA_format, JA_channels, 1024);
|
||||
}
|
||||
|
||||
void JA_Quit() {
|
||||
Mix_CloseAudio();
|
||||
}
|
||||
|
||||
JA_Music_t *JA_LoadMusic(const char* filename) {
|
||||
return (JA_Music_t*)Mix_LoadMUS(filename);
|
||||
}
|
||||
|
||||
void JA_PlayMusic(JA_Music_t *music, const int loop) {
|
||||
Mix_PlayMusic((Mix_Music*)music, loop);
|
||||
}
|
||||
|
||||
void JA_PauseMusic() {
|
||||
Mix_PauseMusic();
|
||||
}
|
||||
|
||||
void JA_ResumeMusic() {
|
||||
Mix_ResumeMusic();
|
||||
}
|
||||
|
||||
void JA_StopMusic() {
|
||||
Mix_HaltMusic();
|
||||
}
|
||||
|
||||
JA_Music_state JA_GetMusicState() {
|
||||
if (Mix_PausedMusic()) {
|
||||
return JA_MUSIC_PAUSED;
|
||||
} else if (Mix_PlayingMusic()) {
|
||||
return JA_MUSIC_PLAYING;
|
||||
} else {
|
||||
return JA_MUSIC_STOPPED;
|
||||
}
|
||||
}
|
||||
|
||||
void JA_DeleteMusic(JA_Music_t *music) {
|
||||
Mix_FreeMusic((Mix_Music*)music);
|
||||
}
|
||||
|
||||
JA_Sound_t *JA_NewSound(Uint8* buffer, Uint32 length) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
JA_Sound_t *JA_LoadSound(const char* filename) {
|
||||
JA_Sound_t *sound = (JA_Sound_t*)Mix_LoadWAV(filename);
|
||||
return sound;
|
||||
}
|
||||
|
||||
int JA_PlaySound(JA_Sound_t *sound, const int loop) {
|
||||
return Mix_PlayChannel(-1, (Mix_Chunk*)sound, loop);
|
||||
}
|
||||
|
||||
void JA_DeleteSound(JA_Sound_t *sound) {
|
||||
Mix_FreeChunk((Mix_Chunk*)sound);
|
||||
}
|
||||
|
||||
void JA_PauseChannel(const int channel) {
|
||||
Mix_Pause(channel);
|
||||
}
|
||||
|
||||
void JA_ResumeChannel(const int channel) {
|
||||
Mix_Resume(channel);
|
||||
}
|
||||
|
||||
void JA_StopChannel(const int channel) {
|
||||
Mix_HaltChannel(channel);
|
||||
}
|
||||
|
||||
JA_Channel_state JA_GetChannelState(const int channel) {
|
||||
if (Mix_Paused(channel)) {
|
||||
return JA_CHANNEL_PAUSED;
|
||||
} else if (Mix_Playing(channel)) {
|
||||
return JA_CHANNEL_PLAYING;
|
||||
} else {
|
||||
return JA_CHANNEL_FREE;
|
||||
}
|
||||
}
|
||||
|
||||
int JA_SetVolume(int volume) {
|
||||
return Mix_Volume(-1, volume);
|
||||
}
|
||||
#endif
|
||||
@@ -1,154 +0,0 @@
|
||||
#include "jscore.h"
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <sys/types.h>
|
||||
#ifdef _WIN32
|
||||
#include <winsock2.h>
|
||||
#else
|
||||
#include <sys/socket.h>
|
||||
#include <netdb.h>
|
||||
#include <netinet/in.h>
|
||||
#endif
|
||||
#include <unistd.h>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
namespace jscore {
|
||||
|
||||
using namespace std;
|
||||
struct user {
|
||||
string name;
|
||||
int points;
|
||||
};
|
||||
vector<user> score;
|
||||
|
||||
#define bzero(b,len) (memset((b), '\0', (len)), (void) 0)
|
||||
int sock;
|
||||
struct sockaddr_in client;
|
||||
|
||||
int PORT = 9911;
|
||||
string HOST = "jaildoctor.duckdns.org";
|
||||
|
||||
#ifdef WIN32
|
||||
WSADATA WsaData;
|
||||
#endif
|
||||
|
||||
bool jscore_error = false;
|
||||
string error_message;
|
||||
|
||||
void init(std::string host, const int port) {
|
||||
PORT = port;
|
||||
HOST = host;
|
||||
}
|
||||
|
||||
void setErrorMessage(string message) {
|
||||
jscore_error = true;
|
||||
error_message = message;
|
||||
}
|
||||
|
||||
string sendRequest(const string request) {
|
||||
#ifdef WIN32
|
||||
int ret = WSAStartup(0x101,&WsaData);
|
||||
if (ret != 0) return 0;
|
||||
#endif
|
||||
struct hostent * host = gethostbyname(HOST.c_str());
|
||||
|
||||
if ( (host == NULL) || (host->h_addr == NULL) ) {
|
||||
setErrorMessage("Error retrieving DNS information.\n");
|
||||
return "";
|
||||
}
|
||||
|
||||
bzero(&client, sizeof(client));
|
||||
client.sin_family = AF_INET;
|
||||
client.sin_port = htons( PORT );
|
||||
memcpy(&client.sin_addr, host->h_addr, host->h_length);
|
||||
|
||||
sock = socket(AF_INET, SOCK_STREAM, 0);
|
||||
|
||||
if (sock < 0) {
|
||||
setErrorMessage("Error creating socket.\n");
|
||||
return "";
|
||||
}
|
||||
|
||||
if ( connect(sock, (struct sockaddr *)&client, sizeof(client)) < 0 ) {
|
||||
close(sock);
|
||||
setErrorMessage("Could not connect\n");
|
||||
return "";
|
||||
}
|
||||
|
||||
string r = request + " HTTP/1.1\r\nHost: "+HOST+"\r\nConnection: close\r\n\r\n\r\n";
|
||||
if (send(sock, r.c_str(), r.length(), 0) != (int)r.length()) {
|
||||
setErrorMessage("Error sending request.\n");
|
||||
return "";
|
||||
}
|
||||
|
||||
char cur;
|
||||
char start[5]="\r\n\r\n";
|
||||
int pos = 0;
|
||||
while ( recv(sock, &cur, 1,0) > 0 ) {
|
||||
if (cur==start[pos]) { pos++; if (pos == 4) break; } else { pos = 0; }
|
||||
}
|
||||
|
||||
char buffer[1024]; buffer[0]=0; pos=0;
|
||||
while ( recv(sock, &cur, 1,0) > 0 ) {
|
||||
buffer[pos] = cur;
|
||||
pos++;
|
||||
}
|
||||
#ifdef WIN32
|
||||
WSACleanup();
|
||||
#endif
|
||||
buffer[pos]=0;
|
||||
return buffer;
|
||||
}
|
||||
|
||||
const bool initOnlineScore(string game) {
|
||||
string strbuff = sendRequest("GET /score-list.php?game=" + game);
|
||||
if (jscore_error) return not jscore_error;
|
||||
|
||||
user u;
|
||||
char buffer[1024];
|
||||
strcpy(buffer, strbuff.c_str());
|
||||
char *str = buffer;
|
||||
char *p = str;
|
||||
score.clear();
|
||||
while (*p!=0) {
|
||||
while (*p!=',') {p++;}
|
||||
*p=0; u.name = str; p++; str=p;
|
||||
while (*p!='\n') {p++;}
|
||||
*p=0; u.points = atoi(str); p++; str=p;
|
||||
score.push_back(u);
|
||||
}
|
||||
return not jscore_error;
|
||||
}
|
||||
|
||||
const int getNumUsers() {
|
||||
return score.size();
|
||||
}
|
||||
string getUserName(const int index) {
|
||||
return score[index].name;
|
||||
}
|
||||
const int getPoints(const int index) {
|
||||
return score[index].points;
|
||||
}
|
||||
|
||||
const bool updateUserPoints(string game, string user, const int points) {
|
||||
string strbuff = sendRequest("GET /score-update.php?game=" + game + "&user=" + user + "&points=" + to_string(points));
|
||||
initOnlineScore(game);
|
||||
return not jscore_error;
|
||||
}
|
||||
|
||||
const int getUserPoints(string game, std::string user) {
|
||||
return atoi(sendRequest("GET /getuserpoints.php?game=" + game + "&user=" + user).c_str());
|
||||
}
|
||||
|
||||
string getUserData(string game, string user) {
|
||||
return sendRequest("GET /getuserdata.php?game=" + game + "&user=" + user);
|
||||
}
|
||||
|
||||
void setUserData(string game, string user, string data) {
|
||||
sendRequest("GET /setuserdata.php?game=" + game + "&user=" + user + "&data=" + data);
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
@@ -1,16 +0,0 @@
|
||||
#pragma once
|
||||
#include <string>
|
||||
|
||||
namespace jscore {
|
||||
void init(std::string host, const int port);
|
||||
const bool initOnlineScore(std::string game);
|
||||
const int getNumUsers();
|
||||
std::string getUserName(const int index);
|
||||
const int getPoints(const int index);
|
||||
const int getUserPoints(std::string game, std::string user);
|
||||
|
||||
const bool updateUserPoints(std::string game, std::string user, const int points);
|
||||
std::string getUserData(std::string game, std::string user);
|
||||
void setUserData(std::string game, std::string user, std::string data);
|
||||
};
|
||||
|
||||
@@ -1,982 +0,0 @@
|
||||
#include "../const.h"
|
||||
#include "menu.h"
|
||||
|
||||
// Constructor
|
||||
Menu::Menu(SDL_Renderer *renderer, Resource *resource, Asset *asset, Input *input, std::string file)
|
||||
{
|
||||
// Copia punteros
|
||||
this->renderer = renderer;
|
||||
this->asset = asset;
|
||||
this->input = input;
|
||||
|
||||
// Inicializa punteros
|
||||
soundMove = nullptr;
|
||||
soundAccept = nullptr;
|
||||
soundCancel = nullptr;
|
||||
|
||||
// Inicializa variables
|
||||
name = "";
|
||||
selector.index = 0;
|
||||
itemSelected = MENU_NO_OPTION;
|
||||
x = 0;
|
||||
y = 0;
|
||||
w = 0;
|
||||
rectBG.rect = {0, 0, 0, 0};
|
||||
rectBG.color = {0, 0, 0};
|
||||
rectBG.a = 0;
|
||||
backgroundType = MENU_BACKGROUND_SOLID;
|
||||
isCenteredOnX = false;
|
||||
isCenteredOnY = false;
|
||||
areElementsCenteredOnX = false;
|
||||
centerX = 0;
|
||||
centerY = 0;
|
||||
widestItem = 0;
|
||||
colorGreyed = {128, 128, 128};
|
||||
defaultActionWhenCancel = 0;
|
||||
font_png = "";
|
||||
font_txt = "";
|
||||
|
||||
// Selector
|
||||
selector.originY = 0;
|
||||
selector.targetY = 0;
|
||||
selector.despY = 0;
|
||||
selector.originH = 0;
|
||||
selector.targetH = 0;
|
||||
selector.incH = 0;
|
||||
selector.y = 0.0f;
|
||||
selector.h = 0.0f;
|
||||
selector.numJumps = 8;
|
||||
selector.moving = false;
|
||||
selector.resizing = false;
|
||||
selector.rect = {0, 0, 0, 0};
|
||||
selector.color = {0, 0, 0};
|
||||
selector.itemColor = {0, 0, 0};
|
||||
selector.a = 255;
|
||||
|
||||
// Inicializa las variables desde un fichero
|
||||
if (file != "")
|
||||
{
|
||||
load(file);
|
||||
}
|
||||
|
||||
// Deja el cursor en el primer elemento
|
||||
reset();
|
||||
}
|
||||
|
||||
Menu::~Menu()
|
||||
{
|
||||
renderer = nullptr;
|
||||
asset = nullptr;
|
||||
input = nullptr;
|
||||
|
||||
if (soundMove)
|
||||
{
|
||||
JA_DeleteSound(soundMove);
|
||||
}
|
||||
|
||||
if (soundAccept)
|
||||
{
|
||||
JA_DeleteSound(soundAccept);
|
||||
}
|
||||
|
||||
if (soundCancel)
|
||||
{
|
||||
JA_DeleteSound(soundCancel);
|
||||
}
|
||||
|
||||
if (text != nullptr)
|
||||
{
|
||||
delete text;
|
||||
}
|
||||
}
|
||||
|
||||
// Carga la configuración del menu desde un archivo de texto
|
||||
bool Menu::load(std::string file_path)
|
||||
{
|
||||
// Indicador de éxito en la carga
|
||||
bool success = true;
|
||||
|
||||
// Indica si se ha creado ya el objeto de texto
|
||||
bool textAllocated = false;
|
||||
|
||||
const std::string filename = file_path.substr(file_path.find_last_of("\\/") + 1);
|
||||
std::string line;
|
||||
std::ifstream file(file_path);
|
||||
|
||||
// El fichero se puede abrir
|
||||
if (file.good())
|
||||
{
|
||||
// Procesa el fichero linea a linea
|
||||
std::cout << "Reading file " << filename.c_str() << std::endl;
|
||||
while (std::getline(file, line))
|
||||
{
|
||||
if (line == "[item]")
|
||||
{
|
||||
item_t item;
|
||||
item.label = "";
|
||||
item.hPaddingDown = 1;
|
||||
item.selectable = true;
|
||||
item.greyed = false;
|
||||
item.linkedDown = false;
|
||||
|
||||
do
|
||||
{
|
||||
// Lee la siguiente linea
|
||||
std::getline(file, line);
|
||||
|
||||
// Encuentra la posición del caracter '='
|
||||
int pos = line.find("=");
|
||||
|
||||
// Procesa las dos subcadenas
|
||||
if (!setItem(&item, line.substr(0, pos), line.substr(pos + 1, line.length())))
|
||||
{
|
||||
std::cout << "Warning: file " << filename.c_str() << "\n, unknown parameter \"" << line.substr(0, pos).c_str() << "\"" << std::endl;
|
||||
success = false;
|
||||
}
|
||||
|
||||
} while (line != "[/item]");
|
||||
|
||||
addItem(item.label, item.hPaddingDown, item.selectable, item.greyed, item.linkedDown);
|
||||
}
|
||||
|
||||
// En caso contrario se parsea el fichero para buscar las variables y los valores
|
||||
else
|
||||
{
|
||||
// Encuentra la posición del caracter '='
|
||||
int pos = line.find("=");
|
||||
// Procesa las dos subcadenas
|
||||
if (!setVars(line.substr(0, pos), line.substr(pos + 1, line.length())))
|
||||
{
|
||||
std::cout << "Warning: file " << filename.c_str() << "\n, unknown parameter \"" << line.substr(0, pos).c_str() << "\"" << std::endl;
|
||||
success = false;
|
||||
}
|
||||
|
||||
// Crea el objeto text tan pronto como se pueda. Necesario para añadir items
|
||||
if (font_png != "" && font_txt != "" && !textAllocated)
|
||||
{
|
||||
text = new Text(resource->getOffset(font_txt), resource->getTexture(font_png), renderer);
|
||||
textAllocated = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Cierra el fichero
|
||||
std::cout << "Closing file " << filename.c_str() << std::endl;
|
||||
file.close();
|
||||
}
|
||||
// El fichero no se puede abrir
|
||||
else
|
||||
{
|
||||
std::cout << "Warning: Unable to open " << filename.c_str() << " file" << std::endl;
|
||||
success = false;
|
||||
}
|
||||
|
||||
return success;
|
||||
}
|
||||
|
||||
// Asigna variables a partir de dos cadenas
|
||||
bool Menu::setItem(item_t *item, std::string var, std::string value)
|
||||
{
|
||||
// Indicador de éxito en la asignación
|
||||
bool success = true;
|
||||
|
||||
if (var == "text")
|
||||
{
|
||||
item->label = value;
|
||||
}
|
||||
|
||||
else if (var == "hPaddingDown")
|
||||
{
|
||||
item->hPaddingDown = std::stoi(value);
|
||||
}
|
||||
|
||||
else if (var == "selectable")
|
||||
{
|
||||
item->selectable = value == "true" ? true : false;
|
||||
}
|
||||
|
||||
else if (var == "greyed")
|
||||
{
|
||||
item->greyed = value == "true" ? true : false;
|
||||
}
|
||||
|
||||
else if (var == "linkedDown")
|
||||
{
|
||||
item->linkedDown = value == "true" ? true : false;
|
||||
}
|
||||
|
||||
else if ((var == "") || (var == "[/item]"))
|
||||
{
|
||||
}
|
||||
|
||||
else
|
||||
{
|
||||
success = false;
|
||||
}
|
||||
|
||||
return success;
|
||||
}
|
||||
|
||||
// Asigna variables a partir de dos cadenas
|
||||
bool Menu::setVars(std::string var, std::string value)
|
||||
{
|
||||
// Indicador de éxito en la asignación
|
||||
bool success = true;
|
||||
|
||||
if (var == "font_png")
|
||||
{
|
||||
font_png = value;
|
||||
}
|
||||
|
||||
else if (var == "font_txt")
|
||||
{
|
||||
font_txt = value;
|
||||
}
|
||||
|
||||
else if (var == "sound_cancel")
|
||||
{
|
||||
soundCancel = JA_LoadSound(asset->get(value).c_str());
|
||||
}
|
||||
|
||||
else if (var == "sound_accept")
|
||||
{
|
||||
soundAccept = JA_LoadSound(asset->get(value).c_str());
|
||||
}
|
||||
|
||||
else if (var == "sound_move")
|
||||
{
|
||||
soundMove = JA_LoadSound(asset->get(value).c_str());
|
||||
}
|
||||
|
||||
else if (var == "name")
|
||||
{
|
||||
name = value;
|
||||
}
|
||||
|
||||
else if (var == "x")
|
||||
{
|
||||
x = std::stoi(value);
|
||||
}
|
||||
|
||||
else if (var == "centerX")
|
||||
{
|
||||
centerX = std::stoi(value);
|
||||
}
|
||||
|
||||
else if (var == "centerY")
|
||||
{
|
||||
centerY = std::stoi(value);
|
||||
}
|
||||
|
||||
else if (var == "y")
|
||||
{
|
||||
y = std::stoi(value);
|
||||
}
|
||||
|
||||
else if (var == "backgroundType")
|
||||
{
|
||||
backgroundType = std::stoi(value);
|
||||
}
|
||||
|
||||
else if (var == "backgroundColor")
|
||||
{
|
||||
// Se introducen los valores separados por comas en un vector
|
||||
std::stringstream ss(value);
|
||||
std::string tmp;
|
||||
getline(ss, tmp, ',');
|
||||
rectBG.color.r = std::stoi(tmp);
|
||||
getline(ss, tmp, ',');
|
||||
rectBG.color.g = std::stoi(tmp);
|
||||
getline(ss, tmp, ',');
|
||||
rectBG.color.b = std::stoi(tmp);
|
||||
getline(ss, tmp, ',');
|
||||
rectBG.a = std::stoi(tmp);
|
||||
}
|
||||
|
||||
else if (var == "selector_color")
|
||||
{
|
||||
// Se introducen los valores separados por comas en un vector
|
||||
std::stringstream ss(value);
|
||||
std::string tmp;
|
||||
getline(ss, tmp, ',');
|
||||
selector.color.r = std::stoi(tmp);
|
||||
getline(ss, tmp, ',');
|
||||
selector.color.g = std::stoi(tmp);
|
||||
getline(ss, tmp, ',');
|
||||
selector.color.b = std::stoi(tmp);
|
||||
getline(ss, tmp, ',');
|
||||
selector.a = std::stoi(tmp);
|
||||
}
|
||||
|
||||
else if (var == "selector_text_color")
|
||||
{
|
||||
// Se introducen los valores separados por comas en un vector
|
||||
std::stringstream ss(value);
|
||||
std::string tmp;
|
||||
getline(ss, tmp, ',');
|
||||
selector.itemColor.r = std::stoi(tmp);
|
||||
getline(ss, tmp, ',');
|
||||
selector.itemColor.g = std::stoi(tmp);
|
||||
getline(ss, tmp, ',');
|
||||
selector.itemColor.b = std::stoi(tmp);
|
||||
}
|
||||
|
||||
else if (var == "areElementsCenteredOnX")
|
||||
{
|
||||
areElementsCenteredOnX = value == "true" ? true : false;
|
||||
}
|
||||
|
||||
else if (var == "isCenteredOnX")
|
||||
{
|
||||
isCenteredOnX = value == "true" ? true : false;
|
||||
}
|
||||
|
||||
else if (var == "isCenteredOnY")
|
||||
{
|
||||
isCenteredOnY = value == "true" ? true : false;
|
||||
}
|
||||
|
||||
else if (var == "defaultActionWhenCancel")
|
||||
{
|
||||
defaultActionWhenCancel = std::stoi(value);
|
||||
}
|
||||
|
||||
else if (var == "")
|
||||
{
|
||||
}
|
||||
|
||||
else
|
||||
{
|
||||
success = false;
|
||||
}
|
||||
|
||||
return success;
|
||||
}
|
||||
|
||||
// Carga los ficheros de audio
|
||||
void Menu::loadAudioFile(std::string file, int sound)
|
||||
{
|
||||
switch (sound)
|
||||
{
|
||||
case SOUND_ACCEPT:
|
||||
soundAccept = JA_LoadSound(file.c_str());
|
||||
break;
|
||||
|
||||
case SOUND_CANCEL:
|
||||
soundCancel = JA_LoadSound(file.c_str());
|
||||
break;
|
||||
|
||||
case SOUND_MOVE:
|
||||
soundMove = JA_LoadSound(file.c_str());
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// Obtiene el nombre del menu
|
||||
std::string Menu::getName()
|
||||
{
|
||||
return name;
|
||||
}
|
||||
|
||||
// Obtiene el valor de la variable
|
||||
int Menu::getItemSelected()
|
||||
{
|
||||
// Al llamar a esta funcion, se obtiene el valor y se borra
|
||||
const int temp = itemSelected;
|
||||
itemSelected = MENU_NO_OPTION;
|
||||
return temp;
|
||||
}
|
||||
|
||||
// Actualiza la posicion y el estado del selector
|
||||
void Menu::updateSelector()
|
||||
{
|
||||
if (selector.moving)
|
||||
{
|
||||
// Calcula el desplazamiento en Y
|
||||
selector.y += selector.despY;
|
||||
if (selector.despY > 0) // Va hacia abajo
|
||||
{
|
||||
if (selector.y > selector.targetY) // Ha llegado al destino
|
||||
{
|
||||
selector.originY = selector.y = selector.targetY;
|
||||
selector.moving = false;
|
||||
}
|
||||
}
|
||||
|
||||
else if (selector.despY < 0) // Va hacia arriba
|
||||
{
|
||||
if (selector.y < selector.targetY) // Ha llegado al destino
|
||||
{
|
||||
selector.originY = selector.y = selector.targetY;
|
||||
selector.moving = false;
|
||||
}
|
||||
}
|
||||
selector.rect.y = int(selector.y);
|
||||
}
|
||||
else
|
||||
{
|
||||
selector.rect.y = int(selector.y);
|
||||
}
|
||||
|
||||
if (selector.resizing)
|
||||
{
|
||||
// Calcula el incremento en H
|
||||
selector.h += selector.incH;
|
||||
if (selector.incH > 0) // Crece
|
||||
{
|
||||
if (selector.h > selector.targetH) // Ha llegado al destino
|
||||
{
|
||||
// selector.originH = selector.targetH = selector.rect.h = getSelectorHeight(selector.index);
|
||||
selector.originH = selector.h = selector.targetH;
|
||||
selector.resizing = false;
|
||||
}
|
||||
}
|
||||
|
||||
else if (selector.incH < 0) // Decrece
|
||||
{
|
||||
if (selector.h < selector.targetH) // Ha llegado al destino
|
||||
{
|
||||
// selector.originH = selector.targetH = selector.rect.h = getSelectorHeight(selector.index);
|
||||
selector.originH = selector.h = selector.targetH;
|
||||
selector.resizing = false;
|
||||
}
|
||||
}
|
||||
selector.rect.h = int(selector.h);
|
||||
}
|
||||
else
|
||||
{
|
||||
selector.rect.h = getSelectorHeight(selector.index);
|
||||
}
|
||||
}
|
||||
|
||||
// Coloca el selector en una posición específica
|
||||
void Menu::setSelectorPos(int index)
|
||||
{
|
||||
if (index < (int)item.size())
|
||||
{
|
||||
selector.index = index;
|
||||
selector.rect.y = selector.y = selector.originY = selector.targetY = item[selector.index].rect.y;
|
||||
selector.rect.w = rectBG.rect.w;
|
||||
selector.rect.x = rectBG.rect.x;
|
||||
selector.originH = selector.targetH = selector.rect.h = getSelectorHeight(selector.index);
|
||||
selector.moving = false;
|
||||
selector.resizing = false;
|
||||
}
|
||||
}
|
||||
|
||||
// Obtiene la anchura del elemento más ancho del menu
|
||||
int Menu::getWidestItem()
|
||||
{
|
||||
int result = 0;
|
||||
|
||||
// Obtenemos la anchura del item mas ancho
|
||||
for (auto &i : item)
|
||||
{
|
||||
result = std::max(result, i.rect.w);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
// Deja el menu apuntando al primer elemento
|
||||
void Menu::reset()
|
||||
{
|
||||
itemSelected = MENU_NO_OPTION;
|
||||
selector.index = 0;
|
||||
selector.originY = selector.targetY = selector.y = item[0].rect.y;
|
||||
selector.originH = selector.targetH = item[0].rect.h;
|
||||
selector.moving = false;
|
||||
selector.resizing = false;
|
||||
|
||||
// Si el primer elemento no es seleccionable, incrementa el selector
|
||||
if (!item[selector.index].selectable)
|
||||
{
|
||||
increaseSelectorIndex();
|
||||
setSelectorPos(selector.index);
|
||||
}
|
||||
}
|
||||
|
||||
// Actualiza el menu para recolocarlo correctamente y establecer el tamaño
|
||||
void Menu::reorganize()
|
||||
{
|
||||
setRectSize();
|
||||
|
||||
if (isCenteredOnX)
|
||||
{
|
||||
centerMenuOnX(centerX);
|
||||
}
|
||||
|
||||
if (isCenteredOnY)
|
||||
{
|
||||
centerMenuOnY(centerY);
|
||||
}
|
||||
|
||||
if (areElementsCenteredOnX)
|
||||
{
|
||||
centerMenuElementsOnX();
|
||||
}
|
||||
}
|
||||
|
||||
// Deja el menu apuntando al siguiente elemento
|
||||
bool Menu::increaseSelectorIndex()
|
||||
{
|
||||
// Obten las coordenadas del elemento actual
|
||||
selector.y = selector.originY = item[selector.index].rect.y;
|
||||
selector.h = selector.originH = getSelectorHeight(selector.index);
|
||||
|
||||
// Calcula cual es el siguiente elemento
|
||||
++selector.index %= item.size();
|
||||
while (!item[selector.index].selectable)
|
||||
{
|
||||
++selector.index %= item.size();
|
||||
}
|
||||
|
||||
// Establece las coordenadas y altura de destino
|
||||
selector.targetY = item[selector.index].rect.y;
|
||||
selector.despY = (selector.targetY - selector.originY) / selector.numJumps;
|
||||
|
||||
selector.targetH = getSelectorHeight(selector.index);
|
||||
selector.incH = (selector.targetH - selector.originH) / selector.numJumps;
|
||||
|
||||
selector.moving = true;
|
||||
if (selector.incH != 0)
|
||||
{
|
||||
selector.resizing = true;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
// Deja el menu apuntando al elemento anterior
|
||||
bool Menu::decreaseSelectorIndex()
|
||||
{
|
||||
// Obten las coordenadas del elemento actual
|
||||
selector.y = selector.originY = item[selector.index].rect.y;
|
||||
selector.h = selector.originH = getSelectorHeight(selector.index);
|
||||
|
||||
// Calcula cual es el siguiente elemento
|
||||
if (selector.index == 0)
|
||||
{
|
||||
selector.index = item.size() - 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
selector.index--;
|
||||
}
|
||||
|
||||
while (!item[selector.index].selectable)
|
||||
{
|
||||
if (selector.index == 0)
|
||||
{
|
||||
selector.index = item.size() - 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
selector.index--;
|
||||
}
|
||||
}
|
||||
|
||||
// Establece las coordenadas y altura de destino
|
||||
selector.targetY = item[selector.index].rect.y;
|
||||
selector.despY = (selector.targetY - selector.originY) / selector.numJumps;
|
||||
|
||||
selector.targetH = getSelectorHeight(selector.index);
|
||||
selector.incH = (selector.targetH - selector.originH) / selector.numJumps;
|
||||
|
||||
selector.moving = true;
|
||||
if (selector.incH != 0)
|
||||
{
|
||||
selector.resizing = true;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
// Actualiza la logica del menu
|
||||
void Menu::update()
|
||||
{
|
||||
checkInput();
|
||||
updateSelector();
|
||||
}
|
||||
|
||||
// Pinta el menu en pantalla
|
||||
void Menu::render()
|
||||
{
|
||||
// Rendereritza el fondo del menu
|
||||
if (backgroundType == MENU_BACKGROUND_SOLID)
|
||||
{
|
||||
SDL_SetRenderDrawColor(renderer, rectBG.color.r, rectBG.color.g, rectBG.color.b, rectBG.a);
|
||||
SDL_RenderFillRect(renderer, &rectBG.rect);
|
||||
}
|
||||
|
||||
// Renderiza el rectangulo del selector
|
||||
const SDL_Rect temp = {selector.rect.x, selector.rect.y - 1, selector.rect.w, selector.rect.h + 1};
|
||||
SDL_SetRenderDrawColor(renderer, selector.color.r, selector.color.g, selector.color.b, selector.a);
|
||||
SDL_RenderFillRect(renderer, &temp);
|
||||
|
||||
// Renderiza el borde del fondo
|
||||
if (backgroundType == MENU_BACKGROUND_SOLID)
|
||||
{
|
||||
SDL_SetRenderDrawColor(renderer, rectBG.color.r, rectBG.color.g, rectBG.color.b, 255);
|
||||
SDL_RenderDrawRect(renderer, &rectBG.rect);
|
||||
}
|
||||
|
||||
// Renderiza el texto
|
||||
for (int i = 0; i < (int)item.size(); ++i)
|
||||
{
|
||||
if (i == selector.index)
|
||||
{
|
||||
const color_t color = {selector.itemColor.r, selector.itemColor.g, selector.itemColor.b};
|
||||
text->writeColored(item[i].rect.x, item[i].rect.y, item[i].label, color);
|
||||
}
|
||||
|
||||
else if (item[i].selectable)
|
||||
{
|
||||
text->write(item[i].rect.x, item[i].rect.y, item[i].label);
|
||||
}
|
||||
|
||||
else if (item[i].greyed)
|
||||
{
|
||||
text->writeColored(item[i].rect.x, item[i].rect.y, item[i].label, colorGreyed);
|
||||
}
|
||||
|
||||
else
|
||||
{ // No seleccionable
|
||||
if ((item[i].linkedUp) && (i == selector.index + 1))
|
||||
{
|
||||
const color_t color = {selector.itemColor.r, selector.itemColor.g, selector.itemColor.b};
|
||||
text->writeColored(item[i].rect.x, item[i].rect.y, item[i].label, color);
|
||||
}
|
||||
else // No enlazado con el de arriba
|
||||
{
|
||||
text->write(item[i].rect.x, item[i].rect.y, item[i].label);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Establece el rectangulo de fondo del menu y el selector
|
||||
void Menu::setRectSize(int w, int h)
|
||||
{
|
||||
// Establece el ancho
|
||||
if (w == 0)
|
||||
{ // Si no se pasa un valor, se busca si hay uno prefijado
|
||||
if (this->w == 0)
|
||||
{ // Si no hay prefijado, coge el item mas ancho
|
||||
rectBG.rect.w = findWidth() + text->getCharacterSize();
|
||||
}
|
||||
else
|
||||
{ // Si hay prefijado, coge ese
|
||||
rectBG.rect.w = this->w;
|
||||
}
|
||||
}
|
||||
else
|
||||
{ // Si se pasa un valor, se usa y se prefija
|
||||
rectBG.rect.w = w;
|
||||
this->w = w;
|
||||
}
|
||||
|
||||
// Establece el alto
|
||||
if (h == 0)
|
||||
{ // Si no se pasa un valor, se busca de manera automatica
|
||||
rectBG.rect.h = findHeight() + text->getCharacterSize();
|
||||
}
|
||||
else
|
||||
{ // Si se pasa un valor, se aplica
|
||||
rectBG.rect.h = h;
|
||||
}
|
||||
|
||||
// La posición X es la del menú menos medio caracter
|
||||
if (this->w != 0)
|
||||
{ // Si el ancho esta prefijado, la x coinccide
|
||||
rectBG.rect.x = x;
|
||||
}
|
||||
else
|
||||
{ // Si el ancho es automatico, se le da un poco de margen
|
||||
rectBG.rect.x = x - (text->getCharacterSize() / 2);
|
||||
}
|
||||
|
||||
// La posición Y es la del menu menos la altura de medio caracter
|
||||
rectBG.rect.y = y - (text->getCharacterSize() / 2);
|
||||
|
||||
// Establecemos los valores del rectangulo del selector a partir de los valores del rectangulo de fondo
|
||||
setSelectorPos(selector.index);
|
||||
}
|
||||
|
||||
// Establece el color del rectangulo de fondo
|
||||
void Menu::setBackgroundColor(color_t color, int alpha)
|
||||
{
|
||||
rectBG.color = color;
|
||||
rectBG.a = alpha;
|
||||
}
|
||||
|
||||
// Establece el color del rectangulo del selector
|
||||
void Menu::setSelectorColor(color_t color, int alpha)
|
||||
{
|
||||
selector.color = color;
|
||||
selector.a = alpha;
|
||||
}
|
||||
|
||||
// Establece el color del texto del selector
|
||||
void Menu::setSelectorTextColor(color_t color)
|
||||
{
|
||||
selector.itemColor = color;
|
||||
}
|
||||
|
||||
// Centra el menu respecto un punto en el eje X
|
||||
void Menu::centerMenuOnX(int value)
|
||||
{
|
||||
isCenteredOnX = true;
|
||||
if (value != 0)
|
||||
{
|
||||
centerX = value;
|
||||
}
|
||||
else if (centerX == 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
// Establece la nueva posición centrada en funcion del elemento más ancho o del ancho fijo del menu
|
||||
if (w != 0)
|
||||
{ // Si se ha definido un ancho fijo
|
||||
x = (centerX) - (w / 2);
|
||||
}
|
||||
else
|
||||
{ // Si se actua en función del elemento más ancho
|
||||
x = (centerX) - (findWidth() / 2);
|
||||
}
|
||||
|
||||
// Actualiza el rectangulo de fondo y del selector
|
||||
rectBG.rect.x = x;
|
||||
selector.rect.x = x;
|
||||
|
||||
// Reposiciona los elementos del menu
|
||||
for (auto &i : item)
|
||||
{
|
||||
i.rect.x = x;
|
||||
}
|
||||
|
||||
// Recalcula el rectangulo de fondo
|
||||
setRectSize();
|
||||
|
||||
// Vuelve a centrar los elementos si fuera el caso
|
||||
if (areElementsCenteredOnX)
|
||||
{
|
||||
centerMenuElementsOnX();
|
||||
}
|
||||
}
|
||||
|
||||
// Centra el menu respecto un punto en el eje Y
|
||||
void Menu::centerMenuOnY(int value)
|
||||
{
|
||||
isCenteredOnY = true;
|
||||
centerY = value;
|
||||
|
||||
// Establece la nueva posición centrada en funcion del elemento más ancho
|
||||
y = (value) - (findHeight() / 2);
|
||||
|
||||
// Reposiciona los elementos del menu
|
||||
replaceElementsOnY();
|
||||
|
||||
// Recalcula el rectangulo de fondo
|
||||
setRectSize();
|
||||
}
|
||||
|
||||
// Centra los elementos del menu en el eje X
|
||||
void Menu::centerMenuElementsOnX()
|
||||
{
|
||||
areElementsCenteredOnX = true;
|
||||
|
||||
for (auto &i : item)
|
||||
{
|
||||
i.rect.x = (centerX - (i.rect.w / 2));
|
||||
}
|
||||
}
|
||||
|
||||
// Añade un item al menu
|
||||
void Menu::addItem(std::string text, int hPaddingDown, bool selectable, bool greyed, bool linkedDown)
|
||||
{
|
||||
item_t temp;
|
||||
|
||||
if (item.empty())
|
||||
{ // Si es el primer item coge la posición en el eje Y del propio menu
|
||||
temp.rect.y = y;
|
||||
}
|
||||
else
|
||||
{ // En caso contrario, coge la posición en el eje Y a partir del último elemento
|
||||
temp.rect.y = item.back().rect.y + item.back().rect.h + item.back().hPaddingDown;
|
||||
}
|
||||
|
||||
temp.rect.x = x;
|
||||
temp.hPaddingDown = hPaddingDown;
|
||||
temp.selectable = selectable;
|
||||
temp.greyed = greyed;
|
||||
temp.linkedDown = linkedDown;
|
||||
|
||||
item.push_back(temp);
|
||||
|
||||
setItemCaption(item.size() - 1, text);
|
||||
|
||||
if (item.size() > 1)
|
||||
{
|
||||
if (item[item.size() - 2].linkedDown)
|
||||
{
|
||||
item.back().linkedUp = true;
|
||||
}
|
||||
}
|
||||
|
||||
centerX = x + (findWidth() / 2);
|
||||
reorganize();
|
||||
}
|
||||
|
||||
// Cambia el texto de un item
|
||||
void Menu::setItemCaption(int index, std::string text)
|
||||
{
|
||||
item[index].label = text;
|
||||
item[index].rect.w = this->text->lenght(item[index].label);
|
||||
item[index].rect.h = this->text->getCharacterSize();
|
||||
reorganize();
|
||||
}
|
||||
|
||||
// Establece el indice del itemm que se usará por defecto al cancelar el menu
|
||||
void Menu::setDefaultActionWhenCancel(int item)
|
||||
{
|
||||
defaultActionWhenCancel = item;
|
||||
}
|
||||
|
||||
// Gestiona la entrada de teclado y mando durante el menu
|
||||
void Menu::checkInput()
|
||||
{
|
||||
if (input->checkInput(input_up, REPEAT_FALSE))
|
||||
{
|
||||
if (decreaseSelectorIndex())
|
||||
{
|
||||
if (soundMove)
|
||||
{
|
||||
JA_PlaySound(soundMove);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (input->checkInput(input_down, REPEAT_FALSE))
|
||||
{
|
||||
if (increaseSelectorIndex())
|
||||
{
|
||||
if (soundMove)
|
||||
{
|
||||
JA_PlaySound(soundMove);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (input->checkInput(input_accept, REPEAT_FALSE))
|
||||
{
|
||||
itemSelected = selector.index;
|
||||
if (soundAccept)
|
||||
{
|
||||
JA_PlaySound(soundAccept);
|
||||
}
|
||||
}
|
||||
|
||||
if (input->checkInput(input_cancel, REPEAT_FALSE))
|
||||
{
|
||||
itemSelected = defaultActionWhenCancel;
|
||||
if (soundCancel)
|
||||
{
|
||||
JA_PlaySound(soundCancel);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Calcula el ancho del menu
|
||||
int Menu::findWidth()
|
||||
{
|
||||
return getWidestItem();
|
||||
}
|
||||
|
||||
// Calcula el alto del menu
|
||||
int Menu::findHeight()
|
||||
{
|
||||
int height = 0;
|
||||
|
||||
// Obtenemos la altura de la suma de alturas de los items
|
||||
for (auto &i : item)
|
||||
{
|
||||
height += i.rect.h + i.hPaddingDown;
|
||||
}
|
||||
|
||||
return height - item.back().hPaddingDown;
|
||||
}
|
||||
|
||||
// Recoloca los elementos del menu en el eje Y
|
||||
void Menu::replaceElementsOnY()
|
||||
{
|
||||
item[0].rect.y = y;
|
||||
|
||||
for (int i = 1; i < (int)item.size(); i++)
|
||||
{
|
||||
item[i].rect.y = item[i - 1].rect.y + item[i - 1].rect.h + item[i - 1].hPaddingDown;
|
||||
}
|
||||
}
|
||||
|
||||
// Establece el estado seleccionable de un item
|
||||
void Menu::setSelectable(int index, bool value)
|
||||
{
|
||||
item[index].selectable = value;
|
||||
}
|
||||
|
||||
// Establece el estado agrisado de un item
|
||||
void Menu::setGreyed(int index, bool value)
|
||||
{
|
||||
item[index].greyed = value;
|
||||
}
|
||||
|
||||
// Establece el estado de enlace de un item
|
||||
void Menu::setLinkedDown(int index, bool value)
|
||||
{
|
||||
item[index].linkedDown = value;
|
||||
}
|
||||
|
||||
// Calcula la altura del selector
|
||||
int Menu::getSelectorHeight(int value)
|
||||
{
|
||||
if (item[value].linkedDown)
|
||||
{
|
||||
return item[value].rect.h + item[value].hPaddingDown + item[value + 1].rect.h;
|
||||
}
|
||||
else
|
||||
{
|
||||
return item[value].rect.h;
|
||||
}
|
||||
}
|
||||
|
||||
// Establece el nombre del menu
|
||||
void Menu::setName(std::string name)
|
||||
{
|
||||
this->name = name;
|
||||
}
|
||||
|
||||
// Establece la posición del menu
|
||||
void Menu::setPos(int x, int y)
|
||||
{
|
||||
this->x = x;
|
||||
this->y = y;
|
||||
}
|
||||
|
||||
// Establece el tipo de fondo del menu
|
||||
void Menu::setBackgroundType(int value)
|
||||
{
|
||||
backgroundType = value;
|
||||
}
|
||||
|
||||
// Establece la fuente de texto que se utilizará
|
||||
void Menu::setText(std::string font_png, std::string font_txt)
|
||||
{
|
||||
if (!text)
|
||||
{
|
||||
text = new Text(resource->getOffset(font_txt), resource->getTexture(font_png), renderer);
|
||||
}
|
||||
}
|
||||
@@ -1,226 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include <SDL2/SDL.h>
|
||||
#include "asset.h"
|
||||
#include "input.h"
|
||||
#include "jail_audio.h"
|
||||
#include "resource.h"
|
||||
#include "sprite.h"
|
||||
#include "text.h"
|
||||
#include "utils.h"
|
||||
#include <fstream>
|
||||
#include <sstream>
|
||||
#include <vector>
|
||||
|
||||
#ifndef MENU_H
|
||||
#define MENU_H
|
||||
|
||||
// Tipos de fondos para el menu
|
||||
#define MENU_BACKGROUND_TRANSPARENT 0
|
||||
#define MENU_BACKGROUND_SOLID 1
|
||||
|
||||
// Tipos de archivos de audio
|
||||
#define SOUND_ACCEPT 0
|
||||
#define SOUND_MOVE 1
|
||||
#define SOUND_CANCEL 2
|
||||
|
||||
// Opciones de menu
|
||||
#define MENU_NO_OPTION -1
|
||||
|
||||
// Clase Menu
|
||||
class Menu
|
||||
{
|
||||
private:
|
||||
struct rectangle_t
|
||||
{
|
||||
SDL_Rect rect; // Rectangulo
|
||||
color_t color; // Color
|
||||
int a; // Transparencia
|
||||
};
|
||||
|
||||
struct item_t
|
||||
{
|
||||
std::string label; // Texto
|
||||
SDL_Rect rect; // Rectangulo que delimita el elemento
|
||||
int hPaddingDown; // Espaciado bajo el elemento
|
||||
bool selectable; // Indica si se puede seleccionar
|
||||
bool greyed; // Indica si ha de aparecer con otro color mas oscuro
|
||||
bool linkedDown; // Indica si el elemento actual y el siguiente se tratan como uno solo. Afecta al selector
|
||||
bool linkedUp; // Indica si el elemento actual y el anterior se tratan como uno solo. Afecta al selector
|
||||
};
|
||||
|
||||
struct selector_t
|
||||
{
|
||||
float originY; // Coordenada de origen
|
||||
float targetY; // Coordenada de destino
|
||||
float despY; // Cantidad de pixeles que se desplaza el selector en cada salto: (target - origin) / numJumps
|
||||
bool moving; // Indica si el selector está avanzando hacia el destino
|
||||
float originH; // Altura de origen
|
||||
float targetH; // Altura de destino
|
||||
float incH; // Cantidad de pixels que debe incrementar o decrementar el selector en cada salto
|
||||
bool resizing; // Indica si el selector está cambiando de tamaño
|
||||
float y; // Coordenada actual, usado para el desplazamiento
|
||||
float h; // Altura actual, usado para el cambio de tamaño
|
||||
int numJumps; // Numero de pasos preestablecido para llegar al destino
|
||||
int index; // Elemento del menu que tiene el foco
|
||||
SDL_Rect rect; // Rectangulo del selector
|
||||
color_t color; // Color del selector
|
||||
color_t itemColor; // Color del item
|
||||
int a; // Cantidad de transparencia para el rectangulo del selector
|
||||
};
|
||||
|
||||
// Objetos y punteros
|
||||
SDL_Renderer *renderer; // Puntero al renderizador de la ventana
|
||||
Resource *resource; // Objeto con los recursos
|
||||
Asset *asset; // Objeto para gestionar los ficheros de recursos
|
||||
Text *text; // Texto para poder escribir los items del menu
|
||||
Input *input; // Gestor de eventos de entrada de teclado o gamepad
|
||||
|
||||
// Variables
|
||||
std::string name; // Nombre del menu
|
||||
int x; // Posición en el eje X de la primera letra del primer elemento
|
||||
int y; // Posición en el eje Y de la primera letra del primer elemento
|
||||
int h; // Altura del menu
|
||||
int w; // Anchura del menu
|
||||
int itemSelected; // Índice del item del menu que ha sido seleccionado
|
||||
int defaultActionWhenCancel; // Indice del item del menu que se selecciona cuando se cancela el menu
|
||||
int backgroundType; // Tipo de fondo para el menu
|
||||
int centerX; // Centro del menu en el eje X
|
||||
int centerY; // Centro del menu en el eje Y
|
||||
bool isCenteredOnX; // Variable para saber si el menu debe estar centrado respecto a un punto en el eje X
|
||||
bool isCenteredOnY; // Variable para saber si el menu debe estar centrado respecto a un punto en el eje Y
|
||||
bool areElementsCenteredOnX; // Variable para saber si los elementos van centrados en el eje X
|
||||
int widestItem; // Anchura del elemento más ancho
|
||||
JA_Sound_t* soundAccept; // Sonido al aceptar o elegir una opción del menu
|
||||
JA_Sound_t* soundCancel; // Sonido al cancelar el menu
|
||||
JA_Sound_t* soundMove; // Sonido al mover el selector
|
||||
color_t colorGreyed; // Color para los elementos agrisados
|
||||
rectangle_t rectBG; // Rectangulo de fondo del menu
|
||||
std::vector<item_t> item; // Estructura para cada elemento del menu
|
||||
selector_t selector; // Variables para pintar el selector del menu
|
||||
std::string font_png;
|
||||
std::string font_txt;
|
||||
|
||||
// Carga la configuración del menu desde un archivo de texto
|
||||
bool load(std::string file_path);
|
||||
|
||||
// Asigna variables a partir de dos cadenas
|
||||
bool setVars(std::string var, std::string value);
|
||||
|
||||
// Asigna variables a partir de dos cadenas
|
||||
bool setItem(item_t *item, std::string var, std::string value);
|
||||
|
||||
// Actualiza el menu para recolocarlo correctamente y establecer el tamaño
|
||||
void reorganize();
|
||||
|
||||
// Deja el menu apuntando al siguiente elemento
|
||||
bool increaseSelectorIndex();
|
||||
|
||||
// Deja el menu apuntando al elemento anterior
|
||||
bool decreaseSelectorIndex();
|
||||
|
||||
// Actualiza la posicion y el estado del selector
|
||||
void updateSelector();
|
||||
|
||||
// Obtiene la anchura del elemento más ancho del menu
|
||||
int getWidestItem();
|
||||
|
||||
// Gestiona la entrada de teclado y mando durante el menu
|
||||
void checkMenuInput(Menu *menu);
|
||||
|
||||
// Calcula el ancho del menu
|
||||
int findWidth();
|
||||
|
||||
// Calcula el alto del menu
|
||||
int findHeight();
|
||||
|
||||
// Recoloca los elementos del menu en el eje Y
|
||||
void replaceElementsOnY();
|
||||
|
||||
// Calcula la altura del selector
|
||||
int getSelectorHeight(int value);
|
||||
|
||||
public:
|
||||
// Constructor
|
||||
Menu(SDL_Renderer *renderer, Resource *resource, Asset *asset, Input *input, std::string file = "");
|
||||
|
||||
// Destructor
|
||||
~Menu();
|
||||
|
||||
// Carga los ficheros de audio
|
||||
void loadAudioFile(std::string file, int sound);
|
||||
|
||||
// Obtiene el nombre del menu
|
||||
std::string getName();
|
||||
|
||||
// Obtiene el valor de la variable
|
||||
int getItemSelected();
|
||||
|
||||
// Deja el menu apuntando al primer elemento
|
||||
void reset();
|
||||
|
||||
// Gestiona la entrada de teclado y mando durante el menu
|
||||
void checkInput();
|
||||
|
||||
// Actualiza la logica del menu
|
||||
void update();
|
||||
|
||||
// Pinta el menu en pantalla
|
||||
void render();
|
||||
|
||||
// Establece el color del rectangulo de fondo
|
||||
void setBackgroundColor(color_t color, int alpha);
|
||||
|
||||
// Establece el color del rectangulo del selector
|
||||
void setSelectorColor(color_t color, int alpha);
|
||||
|
||||
// Establece el color del texto del selector
|
||||
void setSelectorTextColor(color_t color);
|
||||
|
||||
// Centra el menu respecto a un punto en el eje X
|
||||
void centerMenuOnX(int value = 0);
|
||||
|
||||
// Centra el menu respecto a un punto en el eje Y
|
||||
void centerMenuOnY(int value);
|
||||
|
||||
// Centra los elementos del menu en el eje X
|
||||
void centerMenuElementsOnX();
|
||||
|
||||
// Añade un item al menu
|
||||
void addItem(std::string text, int hPaddingDown = 1, bool selectable = true, bool greyed = false, bool linkedDown = false);
|
||||
|
||||
// Cambia el texto de un item
|
||||
void setItemCaption(int index, std::string text);
|
||||
|
||||
// Establece el indice del item que se usará por defecto al cancelar el menu
|
||||
void setDefaultActionWhenCancel(int item);
|
||||
|
||||
// Coloca el selector en una posición específica
|
||||
void setSelectorPos(int index);
|
||||
|
||||
// Establece el estado seleccionable de un item
|
||||
void setSelectable(int index, bool value);
|
||||
|
||||
// Establece el estado agrisado de un item
|
||||
void setGreyed(int index, bool value);
|
||||
|
||||
// Establece el estado de enlace de un item
|
||||
void setLinkedDown(int index, bool value);
|
||||
|
||||
// Establece el nombre del menu
|
||||
void setName(std::string name);
|
||||
|
||||
// Establece la posición del menu
|
||||
void setPos(int x, int y);
|
||||
|
||||
// Establece el tipo de fondo del menu
|
||||
void setBackgroundType(int value);
|
||||
|
||||
// Establece la fuente de texto que se utilizará
|
||||
void setText(std::string font_png, std::string font_txt);
|
||||
|
||||
// Establece el rectangulo de fondo del menu
|
||||
void setRectSize(int w = 0, int h = 0);
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -1,285 +0,0 @@
|
||||
#include "notify.h"
|
||||
#include <string>
|
||||
#include <stdio.h>
|
||||
#include <iostream>
|
||||
|
||||
// Constructor
|
||||
Notify::Notify(SDL_Renderer *renderer, std::string iconFile, std::string bitmapFile, std::string textFile, std::string soundFile, options_t *options)
|
||||
{
|
||||
// Inicializa variables
|
||||
this->renderer = renderer;
|
||||
this->options = options;
|
||||
bgColor = options->notifications.color;
|
||||
waitTime = 300;
|
||||
|
||||
// Crea objetos
|
||||
iconTexture = new Texture(renderer, iconFile);
|
||||
textTexture = new Texture(renderer, bitmapFile);
|
||||
text = new Text(textFile, textTexture, renderer);
|
||||
sound = JA_LoadSound(soundFile.c_str());
|
||||
}
|
||||
|
||||
// Destructor
|
||||
Notify::~Notify()
|
||||
{
|
||||
// Libera la memoria de los objetos
|
||||
delete textTexture;
|
||||
delete iconTexture;
|
||||
delete text;
|
||||
JA_DeleteSound(sound);
|
||||
|
||||
for (auto notification : notifications)
|
||||
{
|
||||
delete notification.sprite;
|
||||
delete notification.texture;
|
||||
}
|
||||
}
|
||||
|
||||
// Dibuja las notificaciones por pantalla
|
||||
void Notify::render()
|
||||
{
|
||||
for (int i = (int)notifications.size() - 1; i >= 0; --i)
|
||||
{
|
||||
notifications[i].sprite->render();
|
||||
}
|
||||
}
|
||||
|
||||
// Actualiza el estado de las notificaiones
|
||||
void Notify::update()
|
||||
{
|
||||
for (int i = 0; i < (int)notifications.size(); ++i)
|
||||
{
|
||||
// Si la notificación anterior está "saliendo", no hagas nada
|
||||
if (i > 0)
|
||||
{
|
||||
if (notifications[i - 1].state == ns_rising)
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
notifications[i].counter++;
|
||||
|
||||
// Hace sonar la notificación en el primer frame
|
||||
if (notifications[i].counter == 1)
|
||||
{
|
||||
if (options->notifications.sound)
|
||||
{
|
||||
if (notifications[i].state == ns_rising)
|
||||
{ // Reproduce el sonido de la notificación
|
||||
JA_PlaySound(sound);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Comprueba los estados
|
||||
if (notifications[i].state == ns_rising)
|
||||
{
|
||||
const float step = ((float)notifications[i].counter / notifications[i].travelDist);
|
||||
const int alpha = 255 * step;
|
||||
|
||||
if (options->notifications.posV == pos_top)
|
||||
{
|
||||
notifications[i].rect.y++;
|
||||
}
|
||||
else
|
||||
{
|
||||
notifications[i].rect.y--;
|
||||
}
|
||||
notifications[i].texture->setAlpha(alpha);
|
||||
|
||||
if (notifications[i].rect.y == notifications[i].y)
|
||||
{
|
||||
notifications[i].state = ns_stay;
|
||||
notifications[i].texture->setAlpha(255);
|
||||
notifications[i].counter = 0;
|
||||
}
|
||||
}
|
||||
|
||||
else if (notifications[i].state == ns_stay)
|
||||
{
|
||||
if (notifications[i].counter == waitTime)
|
||||
{
|
||||
notifications[i].state = ns_vanishing;
|
||||
notifications[i].counter = 0;
|
||||
}
|
||||
}
|
||||
else if (notifications[i].state == ns_vanishing)
|
||||
{
|
||||
|
||||
const float step = (notifications[i].counter / (float)notifications[i].travelDist);
|
||||
const int alpha = 255 * (1 - step);
|
||||
|
||||
if (options->notifications.posV == pos_top)
|
||||
{
|
||||
notifications[i].rect.y--;
|
||||
}
|
||||
else
|
||||
{
|
||||
notifications[i].rect.y++;
|
||||
}
|
||||
notifications[i].texture->setAlpha(alpha);
|
||||
|
||||
if (notifications[i].rect.y == notifications[i].y - notifications[i].travelDist)
|
||||
{
|
||||
notifications[i].state = ns_finished;
|
||||
}
|
||||
}
|
||||
|
||||
notifications[i].sprite->setRect(notifications[i].rect);
|
||||
}
|
||||
|
||||
clearFinishedNotifications();
|
||||
}
|
||||
|
||||
// Elimina las notificaciones finalizadas
|
||||
void Notify::clearFinishedNotifications()
|
||||
{
|
||||
for (int i = (int)notifications.size() - 1; i >= 0; --i)
|
||||
{
|
||||
if (notifications[i].state == ns_finished)
|
||||
{
|
||||
delete notifications[i].sprite;
|
||||
delete notifications[i].texture;
|
||||
notifications.erase(notifications.begin() + i);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Muestra una notificación de texto por pantalla;
|
||||
void Notify::showText(std::string text1, std::string text2, int icon)
|
||||
{
|
||||
// Inicializa variables
|
||||
const int iconSize = 16;
|
||||
const int padding = text->getCharacterSize();
|
||||
const int iconSpace = icon >= 0 ? iconSize + padding : 0;
|
||||
const std::string txt = text1.length() > text2.length() ? text1 : text2;
|
||||
const int width = text->lenght(txt) + (padding * 2) + iconSpace;
|
||||
const int height = (text->getCharacterSize() * 2) + (padding * 2);
|
||||
|
||||
// Posición horizontal
|
||||
int despH = 0;
|
||||
if (options->notifications.posH == pos_left)
|
||||
{
|
||||
despH = padding;
|
||||
}
|
||||
else if (options->notifications.posH == pos_middle)
|
||||
{
|
||||
despH = ((options->screen.windowWidth * options->windowSize) / 2 - (width / 2));
|
||||
}
|
||||
else
|
||||
{
|
||||
despH = (options->screen.windowWidth * options->windowSize) - width - padding;
|
||||
}
|
||||
|
||||
// Posición vertical
|
||||
int despV = 0;
|
||||
if (options->notifications.posV == pos_top)
|
||||
{
|
||||
despV = padding;
|
||||
}
|
||||
else
|
||||
{
|
||||
despV = (options->screen.windowHeight * options->windowSize) - height - padding;
|
||||
}
|
||||
|
||||
const int travelDist = height + padding;
|
||||
|
||||
// Offset
|
||||
int offset = 0;
|
||||
if (options->notifications.posV == pos_top)
|
||||
{
|
||||
offset = (int)notifications.size() > 0 ? notifications.back().y + travelDist : despV;
|
||||
}
|
||||
else
|
||||
{
|
||||
offset = (int)notifications.size() > 0 ? notifications.back().y - travelDist : despV;
|
||||
}
|
||||
|
||||
// Crea la notificacion
|
||||
notification_t n;
|
||||
|
||||
// Inicializa variables
|
||||
n.y = offset;
|
||||
n.travelDist = travelDist;
|
||||
n.counter = 0;
|
||||
n.state = ns_rising;
|
||||
n.text1 = text1;
|
||||
n.text2 = text2;
|
||||
if (options->notifications.posV == pos_top)
|
||||
{
|
||||
n.rect = {despH, offset - travelDist, width, height};
|
||||
}
|
||||
else
|
||||
{
|
||||
n.rect = {despH, offset + travelDist, width, height};
|
||||
}
|
||||
|
||||
// Crea la textura
|
||||
n.texture = new Texture(renderer);
|
||||
n.texture->createBlank(renderer, width, height, SDL_TEXTUREACCESS_TARGET);
|
||||
n.texture->setBlendMode(SDL_BLENDMODE_BLEND);
|
||||
|
||||
// Prepara para dibujar en la textura
|
||||
n.texture->setAsRenderTarget(renderer);
|
||||
|
||||
// Dibuja el fondo de la notificación
|
||||
SDL_SetRenderDrawColor(renderer, bgColor.r, bgColor.g, bgColor.b, 255);
|
||||
SDL_Rect rect;
|
||||
rect = {4, 0, width - (4 * 2), height};
|
||||
SDL_RenderFillRect(renderer, &rect);
|
||||
|
||||
rect = {4 / 2, 1, width - 4, height - 2};
|
||||
SDL_RenderFillRect(renderer, &rect);
|
||||
|
||||
rect = {1, 4 / 2, width - 2, height - 4};
|
||||
SDL_RenderFillRect(renderer, &rect);
|
||||
|
||||
rect = {0, 4, width, height - (4 * 2)};
|
||||
SDL_RenderFillRect(renderer, &rect);
|
||||
|
||||
// Dibuja el icono de la notificación
|
||||
if (icon >= 0)
|
||||
{
|
||||
Sprite *sp = new Sprite({0, 0, iconSize, iconSize}, iconTexture, renderer);
|
||||
sp->setPos({padding, padding, iconSize, iconSize});
|
||||
sp->setSpriteClip({iconSize * (icon % 10), iconSize * (icon / 10), iconSize, iconSize});
|
||||
sp->render();
|
||||
delete sp;
|
||||
}
|
||||
|
||||
// Escribe el texto de la notificación
|
||||
color_t color = {255, 255, 255};
|
||||
if (text2 != "")
|
||||
{ // Dos lineas de texto
|
||||
text->writeColored(padding + iconSpace, padding, text1, color);
|
||||
text->writeColored(padding + iconSpace, padding + text->getCharacterSize() + 1, text2, color);
|
||||
}
|
||||
else
|
||||
{ // Una linea de texto
|
||||
text->writeColored(padding + iconSpace, (height / 2) - (text->getCharacterSize() / 2), text1, color);
|
||||
}
|
||||
|
||||
// Deja de dibujar en la textura
|
||||
SDL_SetRenderTarget(renderer, nullptr);
|
||||
|
||||
// Crea el sprite de la notificación
|
||||
n.sprite = new Sprite(n.rect, n.texture, renderer);
|
||||
|
||||
// Deja la notificación invisible
|
||||
n.texture->setAlpha(0);
|
||||
|
||||
// Añade la notificación a la lista
|
||||
notifications.push_back(n);
|
||||
}
|
||||
|
||||
// Indica si hay notificaciones activas
|
||||
bool Notify::active()
|
||||
{
|
||||
if ((int)notifications.size() > 0)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
@@ -1,87 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include <SDL2/SDL.h>
|
||||
#include "jail_audio.h"
|
||||
#include "sprite.h"
|
||||
#include "text.h"
|
||||
#include "texture.h"
|
||||
#include "utils.h"
|
||||
#include <vector>
|
||||
|
||||
#ifndef NOTIFY_H
|
||||
#define NOTIFY_H
|
||||
|
||||
class Notify
|
||||
{
|
||||
private:
|
||||
enum notification_state_e
|
||||
{
|
||||
ns_rising,
|
||||
ns_stay,
|
||||
ns_vanishing,
|
||||
ns_finished
|
||||
};
|
||||
|
||||
enum notification_position_e
|
||||
{
|
||||
upperLeft,
|
||||
upperCenter,
|
||||
upperRight,
|
||||
middleLeft,
|
||||
middleRight,
|
||||
bottomLeft,
|
||||
bottomCenter,
|
||||
bottomRight
|
||||
};
|
||||
|
||||
struct notification_t
|
||||
{
|
||||
std::string text1;
|
||||
std::string text2;
|
||||
int counter;
|
||||
notification_state_e state;
|
||||
notification_position_e position;
|
||||
Texture *texture;
|
||||
Sprite *sprite;
|
||||
SDL_Rect rect;
|
||||
int y;
|
||||
int travelDist;
|
||||
};
|
||||
|
||||
// Objetos y punteros
|
||||
SDL_Renderer *renderer; // El renderizador de la ventana
|
||||
Texture *textTexture; // Textura para la fuente de las notificaciones
|
||||
Texture *iconTexture; // Textura para los iconos de las notificaciones
|
||||
Text *text; // Objeto para dibujar texto
|
||||
options_t *options; // Variable con todas las opciones del programa
|
||||
|
||||
// Variables
|
||||
color_t bgColor; // Color de fondo de las notificaciones
|
||||
int waitTime; // Tiempo que se ve la notificación
|
||||
std::vector<notification_t> notifications; // La lista de notificaciones activas
|
||||
JA_Sound_t *sound; // Sonido a reproducir cuando suena la notificación
|
||||
|
||||
// Elimina las notificaciones finalizadas
|
||||
void clearFinishedNotifications();
|
||||
|
||||
public:
|
||||
// Dibuja las notificaciones por pantalla
|
||||
void render();
|
||||
|
||||
// Actualiza el estado de las notificaiones
|
||||
void update();
|
||||
|
||||
// Constructor
|
||||
Notify(SDL_Renderer *renderer, std::string iconFile, std::string bitmapFile, std::string textFile, std::string soundFile, options_t *options);
|
||||
|
||||
// Destructor
|
||||
~Notify();
|
||||
|
||||
// Muestra una notificación de texto por pantalla;
|
||||
void showText(std::string text1 = "", std::string text2 = "", int icon = -1);
|
||||
|
||||
// Indica si hay notificaciones activas
|
||||
bool active();
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -1,463 +0,0 @@
|
||||
#include "screen.h"
|
||||
#include <string>
|
||||
#include <iostream>
|
||||
|
||||
// Constructor
|
||||
Screen::Screen(SDL_Window *window, SDL_Renderer *renderer, Asset *asset, options_t *options)
|
||||
{
|
||||
// Inicializa variables
|
||||
this->window = window;
|
||||
this->renderer = renderer;
|
||||
this->options = options;
|
||||
this->asset = asset;
|
||||
|
||||
// Crea los objetos
|
||||
notify = new Notify(renderer, asset->get("notify.png"), asset->get("smb2.png"), asset->get("smb2.txt"), asset->get("notify.wav"), options);
|
||||
|
||||
gameCanvasWidth = options->gameWidth;
|
||||
gameCanvasHeight = options->gameHeight;
|
||||
notificationLogicalWidth = gameCanvasWidth;
|
||||
notificationLogicalHeight = gameCanvasHeight;
|
||||
|
||||
iniFade();
|
||||
iniSpectrumFade();
|
||||
|
||||
// Define el color del borde para el modo de pantalla completa
|
||||
borderColor = {0x00, 0x00, 0x00};
|
||||
|
||||
// Crea la textura donde se dibujan los graficos del juego
|
||||
gameCanvas = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, gameCanvasWidth, gameCanvasHeight);
|
||||
if (gameCanvas == nullptr)
|
||||
{
|
||||
if (options->console)
|
||||
{
|
||||
std::cout << "gameCanvas could not be created!\nSDL Error: " << SDL_GetError() << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
// Crea la textura donde se dibuja el borde que rodea el area de juego
|
||||
borderCanvas = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, gameCanvasWidth + options->borderWidth * 2, gameCanvasHeight + options->borderHeight * 2);
|
||||
if (borderCanvas == nullptr)
|
||||
{
|
||||
if (options->console)
|
||||
{
|
||||
std::cout << "borderCanvas could not be created!\nSDL Error: " << SDL_GetError() << std::endl;
|
||||
}
|
||||
}
|
||||
setBorderColor(borderColor);
|
||||
|
||||
// Establece el modo de video
|
||||
setVideoMode(options->videoMode);
|
||||
|
||||
// Inicializa variables
|
||||
notifyActive = false;
|
||||
}
|
||||
|
||||
// Destructor
|
||||
Screen::~Screen()
|
||||
{
|
||||
delete notify;
|
||||
SDL_DestroyTexture(gameCanvas);
|
||||
SDL_DestroyTexture(borderCanvas);
|
||||
}
|
||||
|
||||
// Limpia la pantalla
|
||||
void Screen::clean(color_t color)
|
||||
{
|
||||
SDL_SetRenderDrawColor(renderer, color.r, color.g, color.b, 0xFF);
|
||||
SDL_RenderClear(renderer);
|
||||
}
|
||||
|
||||
// Prepara para empezar a dibujar en la textura de juego
|
||||
void Screen::start()
|
||||
{
|
||||
SDL_SetRenderTarget(renderer, gameCanvas);
|
||||
}
|
||||
|
||||
// Prepara para empezar a dibujar en la textura del borde
|
||||
void Screen::startDrawOnBorder()
|
||||
{
|
||||
SDL_SetRenderTarget(renderer, borderCanvas);
|
||||
}
|
||||
|
||||
// Vuelca el contenido del renderizador en pantalla
|
||||
void Screen::blit()
|
||||
{
|
||||
// Vuelve a dejar el renderizador en modo normal
|
||||
SDL_SetRenderTarget(renderer, nullptr);
|
||||
|
||||
// Borra el contenido previo
|
||||
// SDL_SetRenderDrawColor(renderer, borderColor.r, borderColor.g, borderColor.b, 0xFF);
|
||||
// SDL_RenderClear(renderer);
|
||||
|
||||
// Copia la textura del borde en la ventana
|
||||
if (options->borderEnabled)
|
||||
{
|
||||
SDL_RenderCopy(renderer, borderCanvas, nullptr, nullptr);
|
||||
}
|
||||
|
||||
// Copia la textura de juego en la ventana en la posición adecuada
|
||||
SDL_RenderCopy(renderer, gameCanvas, nullptr, &dest);
|
||||
|
||||
// Dibuja las notificaciones
|
||||
renderNotifications();
|
||||
|
||||
// Muestra por pantalla el renderizador
|
||||
SDL_RenderPresent(renderer);
|
||||
}
|
||||
|
||||
// Establece el modo de video
|
||||
void Screen::setVideoMode(int videoMode)
|
||||
{
|
||||
// Aplica el modo de video
|
||||
SDL_SetWindowFullscreen(window, videoMode);
|
||||
|
||||
// Modo ventana
|
||||
if (videoMode == 0)
|
||||
{
|
||||
// Muestra el puntero
|
||||
SDL_ShowCursor(SDL_ENABLE);
|
||||
|
||||
// Esconde la ventana
|
||||
// SDL_HideWindow(window);
|
||||
|
||||
// Modifica el tamaño de la ventana en función del borde
|
||||
if (options->borderEnabled)
|
||||
{
|
||||
windowWidth = gameCanvasWidth + options->borderWidth * 2;
|
||||
windowHeight = gameCanvasHeight + options->borderHeight * 2;
|
||||
dest = {options->borderWidth, options->borderHeight, gameCanvasWidth, gameCanvasHeight};
|
||||
}
|
||||
|
||||
else
|
||||
{
|
||||
windowWidth = gameCanvasWidth;
|
||||
windowHeight = gameCanvasHeight;
|
||||
dest = {0, 0, gameCanvasWidth, gameCanvasHeight};
|
||||
}
|
||||
|
||||
// Modifica el tamaño de la ventana
|
||||
SDL_SetWindowSize(window, windowWidth * options->windowSize, windowHeight * options->windowSize);
|
||||
SDL_SetWindowPosition(window, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED);
|
||||
|
||||
// Muestra la ventana
|
||||
// SDL_ShowWindow(window);
|
||||
}
|
||||
|
||||
// Si está activo el modo de pantalla completa añade el borde
|
||||
else if (videoMode == SDL_WINDOW_FULLSCREEN_DESKTOP)
|
||||
{
|
||||
// Oculta el puntero
|
||||
SDL_ShowCursor(SDL_DISABLE);
|
||||
|
||||
// Obten el alto y el ancho de la ventana
|
||||
SDL_GetWindowSize(window, &windowWidth, &windowHeight);
|
||||
|
||||
// Aplica el escalado al rectangulo donde se pinta la textura del juego
|
||||
if (options->integerScale)
|
||||
{
|
||||
// Calcula el tamaño de la escala máxima
|
||||
int scale = 0;
|
||||
while (((gameCanvasWidth * (scale + 1)) <= windowWidth) && ((gameCanvasHeight * (scale + 1)) <= windowHeight))
|
||||
{
|
||||
scale++;
|
||||
}
|
||||
|
||||
dest.w = gameCanvasWidth * scale;
|
||||
dest.h = gameCanvasHeight * scale;
|
||||
dest.x = (windowWidth - dest.w) / 2;
|
||||
dest.y = (windowHeight - dest.h) / 2;
|
||||
}
|
||||
else if (options->keepAspect)
|
||||
{
|
||||
float ratio = (float)gameCanvasWidth / (float)gameCanvasHeight;
|
||||
if ((windowWidth - gameCanvasWidth) >= (windowHeight - gameCanvasHeight))
|
||||
{
|
||||
dest.h = windowHeight;
|
||||
dest.w = (int)((windowHeight * ratio) + 0.5f);
|
||||
dest.x = (windowWidth - dest.w) / 2;
|
||||
dest.y = (windowHeight - dest.h) / 2;
|
||||
}
|
||||
else
|
||||
{
|
||||
dest.w = windowWidth;
|
||||
dest.h = (int)((windowWidth / ratio) + 0.5f);
|
||||
dest.x = (windowWidth - dest.w) / 2;
|
||||
dest.y = (windowHeight - dest.h) / 2;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
dest.w = windowWidth;
|
||||
dest.h = windowHeight;
|
||||
dest.x = dest.y = 0;
|
||||
}
|
||||
}
|
||||
|
||||
// Modifica el tamaño del renderizador
|
||||
SDL_RenderSetLogicalSize(renderer, windowWidth, windowHeight);
|
||||
|
||||
// Actualiza las opciones
|
||||
options->videoMode = videoMode;
|
||||
options->screen.windowWidth = windowWidth;
|
||||
options->screen.windowHeight = windowHeight;
|
||||
|
||||
// Establece el tamaño de las notificaciones
|
||||
setNotificationSize();
|
||||
}
|
||||
|
||||
// Camibia entre pantalla completa y ventana
|
||||
void Screen::switchVideoMode()
|
||||
{
|
||||
options->videoMode = (options->videoMode == 0) ? SDL_WINDOW_FULLSCREEN_DESKTOP : 0;
|
||||
setVideoMode(options->videoMode);
|
||||
}
|
||||
|
||||
// Cambia el tamaño de la ventana
|
||||
void Screen::setWindowSize(int size)
|
||||
{
|
||||
options->windowSize = size;
|
||||
setVideoMode(0);
|
||||
}
|
||||
|
||||
// Reduce el tamaño de la ventana
|
||||
void Screen::decWindowSize()
|
||||
{
|
||||
--options->windowSize;
|
||||
options->windowSize = std::max(options->windowSize, 1);
|
||||
setVideoMode(0);
|
||||
}
|
||||
|
||||
// Aumenta el tamaño de la ventana
|
||||
void Screen::incWindowSize()
|
||||
{
|
||||
++options->windowSize;
|
||||
options->windowSize = std::min(options->windowSize, 4);
|
||||
setVideoMode(0);
|
||||
}
|
||||
|
||||
// Cambia el color del borde
|
||||
void Screen::setBorderColor(color_t color)
|
||||
{
|
||||
borderColor = color;
|
||||
SDL_SetRenderTarget(renderer, borderCanvas);
|
||||
SDL_SetRenderDrawColor(renderer, borderColor.r, borderColor.g, borderColor.b, 0xFF);
|
||||
SDL_RenderClear(renderer);
|
||||
SDL_SetRenderTarget(renderer, nullptr);
|
||||
}
|
||||
|
||||
// Cambia el tipo de mezcla
|
||||
void Screen::setBlendMode(SDL_BlendMode blendMode)
|
||||
{
|
||||
SDL_SetRenderDrawBlendMode(renderer, blendMode);
|
||||
}
|
||||
|
||||
// Establece el tamaño del borde
|
||||
void Screen::setBorderWidth(int s)
|
||||
{
|
||||
options->borderWidth = s;
|
||||
}
|
||||
|
||||
// Establece el tamaño del borde
|
||||
void Screen::setBorderHeight(int s)
|
||||
{
|
||||
options->borderHeight = s;
|
||||
}
|
||||
|
||||
// Establece si se ha de ver el borde en el modo ventana
|
||||
void Screen::setBorderEnabled(bool value)
|
||||
{
|
||||
options->borderEnabled = value;
|
||||
}
|
||||
|
||||
// Cambia entre borde visible y no visible
|
||||
void Screen::switchBorder()
|
||||
{
|
||||
options->borderEnabled = !options->borderEnabled;
|
||||
setVideoMode(0);
|
||||
}
|
||||
|
||||
// Activa el fade
|
||||
void Screen::setFade()
|
||||
{
|
||||
fade = true;
|
||||
}
|
||||
|
||||
// Comprueba si ha terminado el fade
|
||||
bool Screen::fadeEnded()
|
||||
{
|
||||
if (fade || fadeCounter > 0)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
// Activa el spectrum fade
|
||||
void Screen::setspectrumFade()
|
||||
{
|
||||
spectrumFade = true;
|
||||
}
|
||||
|
||||
// Comprueba si ha terminado el spectrum fade
|
||||
bool Screen::spectrumFadeEnded()
|
||||
{
|
||||
if (spectrumFade || spectrumFadeCounter > 0)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
// Inicializa las variables para el fade
|
||||
void Screen::iniFade()
|
||||
{
|
||||
fade = false;
|
||||
fadeCounter = 0;
|
||||
fadeLenght = 200;
|
||||
}
|
||||
|
||||
// Actualiza el fade
|
||||
void Screen::updateFade()
|
||||
{
|
||||
if (!fade)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
fadeCounter++;
|
||||
if (fadeCounter > fadeLenght)
|
||||
{
|
||||
iniFade();
|
||||
}
|
||||
}
|
||||
|
||||
// Dibuja el fade
|
||||
void Screen::renderFade()
|
||||
{
|
||||
if (!fade)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
const SDL_Rect rect = {0, 0, gameCanvasWidth, gameCanvasHeight};
|
||||
color_t color = {0, 0, 0};
|
||||
const float step = (float)fadeCounter / (float)fadeLenght;
|
||||
const int alpha = 0 + (255 - 0) * step;
|
||||
SDL_SetRenderDrawColor(renderer, color.r, color.g, color.b, alpha);
|
||||
SDL_RenderFillRect(renderer, &rect);
|
||||
}
|
||||
|
||||
// Inicializa las variables para el fade spectrum
|
||||
void Screen::iniSpectrumFade()
|
||||
{
|
||||
spectrumFade = false;
|
||||
spectrumFadeCounter = 0;
|
||||
spectrumFadeLenght = 50;
|
||||
|
||||
spectrumColor.clear();
|
||||
|
||||
// Inicializa el vector de colores
|
||||
const std::vector<std::string> vColors = {"black", "blue", "red", "magenta", "green", "cyan", "yellow", "bright_white"};
|
||||
for (auto v : vColors)
|
||||
{
|
||||
spectrumColor.push_back(stringToColor(options->palette, v));
|
||||
}
|
||||
}
|
||||
|
||||
// Actualiza el spectrum fade
|
||||
void Screen::updateSpectrumFade()
|
||||
{
|
||||
if (!spectrumFade)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
spectrumFadeCounter++;
|
||||
if (spectrumFadeCounter > spectrumFadeLenght)
|
||||
{
|
||||
iniSpectrumFade();
|
||||
SDL_SetTextureColorMod(gameCanvas, 255, 255, 255);
|
||||
}
|
||||
}
|
||||
|
||||
// Dibuja el spectrum fade
|
||||
void Screen::renderSpectrumFade()
|
||||
{
|
||||
if (!spectrumFade)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
const float step = (float)spectrumFadeCounter / (float)spectrumFadeLenght;
|
||||
const int max = spectrumColor.size() - 1;
|
||||
const int index = max + (0 - max) * step;
|
||||
const color_t c = spectrumColor[index];
|
||||
SDL_SetTextureColorMod(gameCanvas, c.r, c.g, c.b);
|
||||
}
|
||||
|
||||
// Actualiza los efectos
|
||||
void Screen::updateFX()
|
||||
{
|
||||
updateFade();
|
||||
updateSpectrumFade();
|
||||
}
|
||||
|
||||
// Dibuja los efectos
|
||||
void Screen::renderFX()
|
||||
{
|
||||
renderFade();
|
||||
renderSpectrumFade();
|
||||
}
|
||||
|
||||
// Actualiza el notificador
|
||||
void Screen::updateNotifier()
|
||||
{
|
||||
notify->update();
|
||||
notifyActive = notify->active();
|
||||
}
|
||||
|
||||
// Muestra una notificación de texto por pantalla;
|
||||
void Screen::showNotification(std::string text1, std::string text2, int icon)
|
||||
{
|
||||
notify->showText(text1, text2, icon);
|
||||
}
|
||||
|
||||
// Dibuja las notificaciones
|
||||
void Screen::renderNotifications()
|
||||
{
|
||||
if (!notifyActive)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
SDL_RenderSetLogicalSize(renderer, notificationLogicalWidth, notificationLogicalHeight);
|
||||
notify->render();
|
||||
SDL_RenderSetLogicalSize(renderer, windowWidth, windowHeight);
|
||||
}
|
||||
|
||||
// Establece el tamaño de las notificaciones
|
||||
void Screen::setNotificationSize()
|
||||
{
|
||||
if (options->videoMode == 0)
|
||||
{
|
||||
if (options->windowSize == 3)
|
||||
{
|
||||
notificationLogicalWidth = (windowWidth * 3) / 2;
|
||||
notificationLogicalHeight = (windowHeight * 3) / 2;
|
||||
}
|
||||
else
|
||||
{
|
||||
notificationLogicalWidth = windowWidth * 2;
|
||||
notificationLogicalHeight = windowHeight * 2;
|
||||
}
|
||||
}
|
||||
|
||||
if (options->videoMode == SDL_WINDOW_FULLSCREEN_DESKTOP)
|
||||
{
|
||||
notificationLogicalWidth = windowWidth / 3;
|
||||
notificationLogicalHeight = windowHeight / 3;
|
||||
}
|
||||
}
|
||||
@@ -1,147 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include <SDL2/SDL.h>
|
||||
#include "asset.h"
|
||||
#include "notify.h"
|
||||
#include "utils.h"
|
||||
#include "../const.h"
|
||||
#include <vector>
|
||||
|
||||
#ifndef SCREEN_H
|
||||
#define SCREEN_H
|
||||
|
||||
#define FILTER_NEAREST 0
|
||||
#define FILTER_LINEAL 1
|
||||
|
||||
class Screen
|
||||
{
|
||||
private:
|
||||
// Objetos y punteros
|
||||
SDL_Window *window; // Ventana de la aplicación
|
||||
SDL_Renderer *renderer; // El renderizador de la ventana
|
||||
Asset *asset; // Objeto con el listado de recursos
|
||||
SDL_Texture *gameCanvas; // Textura donde se dibuja el juego
|
||||
SDL_Texture *borderCanvas; // Textura donde se dibuja el borde del juego
|
||||
options_t *options; // Variable con todas las opciones del programa
|
||||
Notify *notify; // Dibuja notificaciones por pantalla
|
||||
|
||||
// Variables
|
||||
int windowWidth; // Ancho de la pantalla o ventana
|
||||
int windowHeight; // Alto de la pantalla o ventana
|
||||
int gameCanvasWidth; // Resolución interna del juego. Es el ancho de la textura donde se dibuja el juego
|
||||
int gameCanvasHeight; // Resolución interna del juego. Es el alto de la textura donde se dibuja el juego
|
||||
SDL_Rect dest; // Coordenadas donde se va a dibujar la textura del juego sobre la pantalla o ventana
|
||||
color_t borderColor; // Color del borde añadido a la textura de juego para rellenar la pantalla
|
||||
bool notifyActive; // Indica si hay notificaciones activas
|
||||
int notificationLogicalWidth; // Ancho lógico de las notificaciones en relación al tamaño de pantalla
|
||||
int notificationLogicalHeight; // Alto lógico de las notificaciones en relación al tamaño de pantalla
|
||||
|
||||
// Variables - Efectos
|
||||
bool fade; // Indica si esta activo el efecto de fade
|
||||
int fadeCounter; // Temporizador para el efecto de fade
|
||||
int fadeLenght; // Duración del fade
|
||||
bool spectrumFade; // Indica si esta activo el efecto de fade spectrum
|
||||
int spectrumFadeCounter; // Temporizador para el efecto de fade spectrum
|
||||
int spectrumFadeLenght; // Duración del fade spectrum
|
||||
std::vector<color_t> spectrumColor; // Colores para el fade spectrum
|
||||
|
||||
// Inicializa las variables para el fade
|
||||
void iniFade();
|
||||
|
||||
// Actualiza el fade
|
||||
void updateFade();
|
||||
|
||||
// Dibuja el fade
|
||||
void renderFade();
|
||||
|
||||
// Inicializa las variables para el fade spectrum
|
||||
void iniSpectrumFade();
|
||||
|
||||
// Actualiza el spectrum fade
|
||||
void updateSpectrumFade();
|
||||
|
||||
// Dibuja el spectrum fade
|
||||
void renderSpectrumFade();
|
||||
|
||||
// Dibuja las notificaciones
|
||||
void renderNotifications();
|
||||
|
||||
// Establece el tamaño de las notificaciones
|
||||
void setNotificationSize();
|
||||
|
||||
public:
|
||||
// Constructor
|
||||
Screen(SDL_Window *window, SDL_Renderer *renderer, Asset *asset, options_t *options);
|
||||
|
||||
// Destructor
|
||||
~Screen();
|
||||
|
||||
// Limpia la pantalla
|
||||
void clean(color_t color = {0x00, 0x00, 0x00});
|
||||
|
||||
// Prepara para empezar a dibujar en la textura de juego
|
||||
void start();
|
||||
|
||||
// Prepara para empezar a dibujar en la textura del borde
|
||||
void startDrawOnBorder();
|
||||
|
||||
// Vuelca el contenido del renderizador en pantalla
|
||||
void blit();
|
||||
|
||||
// Establece el modo de video
|
||||
void setVideoMode(int videoMode);
|
||||
|
||||
// Camibia entre pantalla completa y ventana
|
||||
void switchVideoMode();
|
||||
|
||||
// Cambia el tamaño de la ventana
|
||||
void setWindowSize(int size);
|
||||
|
||||
// Reduce el tamaño de la ventana
|
||||
void decWindowSize();
|
||||
|
||||
// Aumenta el tamaño de la ventana
|
||||
void incWindowSize();
|
||||
|
||||
// Cambia el color del borde
|
||||
void setBorderColor(color_t color);
|
||||
|
||||
// Cambia el tipo de mezcla
|
||||
void setBlendMode(SDL_BlendMode blendMode);
|
||||
|
||||
// Establece el tamaño del borde
|
||||
void setBorderWidth(int s);
|
||||
void setBorderHeight(int s);
|
||||
|
||||
// Establece si se ha de ver el borde en el modo ventana
|
||||
void setBorderEnabled(bool value);
|
||||
|
||||
// Cambia entre borde visible y no visible
|
||||
void switchBorder();
|
||||
|
||||
// Activa el fade
|
||||
void setFade();
|
||||
|
||||
// Comprueba si ha terminado el fade
|
||||
bool fadeEnded();
|
||||
|
||||
// Activa el spectrum fade
|
||||
void setspectrumFade();
|
||||
|
||||
// Comprueba si ha terminado el spectrum fade
|
||||
bool spectrumFadeEnded();
|
||||
|
||||
// Actualiza los efectos
|
||||
void updateFX();
|
||||
|
||||
// Dibuja los efectos
|
||||
void renderFX();
|
||||
|
||||
// Actualiza el notificador
|
||||
void updateNotifier();
|
||||
|
||||
// Muestra una notificación de texto por pantalla;
|
||||
void showNotification(std::string text1 = "", std::string text2 = "", int icon = -1);
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -1,206 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include <SDL2/SDL.h>
|
||||
#include "texture.h"
|
||||
#include <string>
|
||||
|
||||
#ifndef UTILS_H
|
||||
#define UTILS_H
|
||||
|
||||
// Estructura para definir un circulo
|
||||
struct circle_t
|
||||
{
|
||||
int x;
|
||||
int y;
|
||||
int r;
|
||||
};
|
||||
|
||||
// Estructura para definir una linea horizontal
|
||||
struct h_line_t
|
||||
{
|
||||
int x1, x2, y;
|
||||
};
|
||||
|
||||
// Estructura para definir una linea vertical
|
||||
struct v_line_t
|
||||
{
|
||||
int x, y1, y2;
|
||||
};
|
||||
|
||||
// Estructura para definir una linea diagonal
|
||||
struct d_line_t
|
||||
{
|
||||
int x1, y1, x2, y2;
|
||||
};
|
||||
|
||||
// Estructura para definir una linea
|
||||
struct line_t
|
||||
{
|
||||
int x1, y1, x2, y2;
|
||||
};
|
||||
|
||||
// Estructura para definir un color
|
||||
struct color_t
|
||||
{
|
||||
Uint8 r;
|
||||
Uint8 g;
|
||||
Uint8 b;
|
||||
};
|
||||
|
||||
// Tipos de paleta
|
||||
enum palette_e
|
||||
{
|
||||
p_zxspectrum,
|
||||
p_zxarne
|
||||
};
|
||||
|
||||
// Posiciones de las notificaciones
|
||||
enum not_pos_e
|
||||
{
|
||||
pos_top,
|
||||
pos_bottom,
|
||||
pos_left,
|
||||
pos_middle,
|
||||
pos_right
|
||||
};
|
||||
|
||||
// Tipos de control de teclado
|
||||
enum ctrl_schem_e
|
||||
{
|
||||
ctrl_cursor,
|
||||
ctrl_opqa,
|
||||
ctrl_wasd
|
||||
};
|
||||
|
||||
// Estructura para las opciones de las notificaciones
|
||||
struct op_notification_t
|
||||
{
|
||||
not_pos_e posH; // Ubicación de las notificaciones en pantalla
|
||||
not_pos_e posV; // Ubicación de las notificaciones en pantalla
|
||||
bool sound; // Indica si las notificaciones suenan
|
||||
color_t color; // Color de las notificaciones
|
||||
};
|
||||
|
||||
// Estructura para saber la seccion y subseccion del programa
|
||||
struct section_t
|
||||
{
|
||||
Uint8 name;
|
||||
Uint8 subsection;
|
||||
};
|
||||
|
||||
// Estructura para albergar trucos
|
||||
struct cheat_t
|
||||
{
|
||||
bool infiniteLives; // Indica si el jugador dispone de vidas infinitas
|
||||
bool invincible; // Indica si el jugador puede morir
|
||||
bool jailEnabled; // Indica si la Jail está abierta
|
||||
bool altSkin; // Indicxa si se usa una skin diferente para el jugador
|
||||
};
|
||||
|
||||
// Estructura para el servicio online
|
||||
struct online_t
|
||||
{
|
||||
bool enabled; // Indica si se quiere usar el modo online o no
|
||||
bool sessionEnabled; // Indica ya se ha hecho login
|
||||
std::string server; // Servidor para los servicios online
|
||||
int port; // Puerto del servidor
|
||||
std::string gameID; // Identificador del juego para los servicios online
|
||||
std::string jailerID; // Identificador del jugador para los servicios online
|
||||
int score; // Puntuación almacenada online
|
||||
};
|
||||
|
||||
// Estructura para almacenar estadísticas
|
||||
struct op_stats_t
|
||||
{
|
||||
int rooms; // Cantidad de habitaciones visitadas
|
||||
int items; // Cantidad de items obtenidos
|
||||
std::string worstNightmare; // Habitación con más muertes acumuladas
|
||||
};
|
||||
|
||||
// Estructura con opciones de la pantalla
|
||||
struct op_screen_t
|
||||
{
|
||||
int windowWidth; // Ancho de la ventana
|
||||
int windowHeight; // Alto de la ventana
|
||||
};
|
||||
|
||||
// Estructura con todas las opciones de configuración del programa
|
||||
struct options_t
|
||||
{
|
||||
std::string configVersion; // Versión del programa. Sirve para saber si las opciones son compatibles
|
||||
Uint32 videoMode; // Contiene el valor del modo de pantalla completa
|
||||
int windowSize; // Contiene el valor por el que se multiplica el tamaño de la ventana
|
||||
Uint32 filter; // Filtro usado para el escalado de la imagen
|
||||
bool vSync; // Indica si se quiere usar vsync o no
|
||||
int gameWidth; // Ancho de la resolucion nativa del juego
|
||||
int gameHeight; // Alto de la resolucion nativa del juego
|
||||
bool integerScale; // Indica si el escalado de la imagen ha de ser entero en el modo a pantalla completa
|
||||
bool keepAspect; // Indica si se ha de mantener la relación de aspecto al poner el modo a pantalla completa
|
||||
bool borderEnabled; // Indica si ha de mostrar el borde en el modo de ventana
|
||||
int borderWidth; // Cantidad de pixels que se añade en el borde de la ventana
|
||||
int borderHeight; // Cantidad de pixels que se añade en el borde de la ventana
|
||||
palette_e palette; // Paleta de colores a usar en el juego
|
||||
bool console; // Indica si ha de mostrar información por la consola de texto
|
||||
cheat_t cheat; // Contiene trucos y ventajas para el juego
|
||||
op_stats_t stats; // Datos con las estadisticas de juego
|
||||
online_t online; // Datos del servicio online
|
||||
op_notification_t notifications; // Opciones relativas a las notificaciones;
|
||||
op_screen_t screen; // Opciones relativas a la clase screen
|
||||
ctrl_schem_e keys; // Teclas usadas para jugar
|
||||
};
|
||||
|
||||
// Calcula el cuadrado de la distancia entre dos puntos
|
||||
double distanceSquared(int x1, int y1, int x2, int y2);
|
||||
|
||||
// Detector de colisiones entre dos circulos
|
||||
bool checkCollision(circle_t &a, circle_t &b);
|
||||
|
||||
// Detector de colisiones entre un circulo y un rectangulo
|
||||
bool checkCollision(circle_t &a, SDL_Rect &b);
|
||||
|
||||
// Detector de colisiones entre un dos rectangulos
|
||||
bool checkCollision(SDL_Rect &a, SDL_Rect &b);
|
||||
|
||||
// Detector de colisiones entre un punto y un rectangulo
|
||||
bool checkCollision(SDL_Point &p, SDL_Rect &r);
|
||||
|
||||
// Detector de colisiones entre una linea horizontal y un rectangulo
|
||||
bool checkCollision(h_line_t &l, SDL_Rect &r);
|
||||
|
||||
// Detector de colisiones entre una linea vertical y un rectangulo
|
||||
bool checkCollision(v_line_t &l, SDL_Rect &r);
|
||||
|
||||
// Detector de colisiones entre una linea horizontal y un punto
|
||||
bool checkCollision(h_line_t &l, SDL_Point &p);
|
||||
|
||||
// Detector de colisiones entre dos lineas
|
||||
SDL_Point checkCollision(line_t &l1, line_t &l2);
|
||||
|
||||
// Detector de colisiones entre dos lineas
|
||||
SDL_Point checkCollision(d_line_t &l1, v_line_t &l2);
|
||||
|
||||
// Detector de colisiones entre un punto y una linea diagonal
|
||||
bool checkCollision(SDL_Point &p, d_line_t &l);
|
||||
|
||||
// Normaliza una linea diagonal
|
||||
void normalizeLine(d_line_t &l);
|
||||
|
||||
// Devuelve un color_t a partir de un string
|
||||
color_t stringToColor(palette_e pal, std::string str);
|
||||
|
||||
// Convierte una cadena en un valor booleano
|
||||
bool stringToBool(std::string str);
|
||||
|
||||
// Convierte un valor booleano en una cadena
|
||||
std::string boolToString(bool value);
|
||||
|
||||
// Compara dos colores
|
||||
bool colorAreEqual(color_t color1, color_t color2);
|
||||
|
||||
// Convierte una cadena a minusculas
|
||||
std::string toLower(std::string str);
|
||||
|
||||
// Convierte una cadena a mayúsculas
|
||||
std::string toUpper(std::string str);
|
||||
|
||||
#endif
|
||||
248
source/jail_shader.cpp
Normal file
248
source/jail_shader.cpp
Normal file
@@ -0,0 +1,248 @@
|
||||
#include "jail_shader.h"
|
||||
#include <SDL2/SDL_rect.h> // para SDL_Point
|
||||
#include <stdlib.h> // para NULL, free, malloc, exit
|
||||
#include <string.h> // para strncmp
|
||||
#include <iostream> // para basic_ostream, char_traits, operator<<
|
||||
|
||||
#ifdef __APPLE__
|
||||
#include "CoreFoundation/CoreFoundation.h"
|
||||
#include <OpenGL/OpenGL.h>
|
||||
|
||||
#if ESSENTIAL_GL_PRACTICES_SUPPORT_GL3
|
||||
#include <OpenGL/gl3.h>
|
||||
#else
|
||||
#include <OpenGL/gl.h>
|
||||
#endif //! ESSENTIAL_GL_PRACTICES_SUPPORT_GL3
|
||||
#else
|
||||
#include <SDL2/SDL_opengl.h>
|
||||
#endif
|
||||
|
||||
namespace shader
|
||||
{
|
||||
SDL_Window *win = nullptr;
|
||||
SDL_Renderer *renderer = nullptr;
|
||||
GLuint programId = 0;
|
||||
SDL_Texture *backBuffer = nullptr;
|
||||
SDL_Point win_size = {320 * 4, 256 * 4};
|
||||
SDL_Point tex_size = {320, 256};
|
||||
bool usingOpenGL;
|
||||
|
||||
#ifndef __APPLE__
|
||||
|
||||
// I'm avoiding the use of GLEW or some extensions handler, but that
|
||||
// doesn't mean you should...
|
||||
PFNGLCREATESHADERPROC glCreateShader;
|
||||
PFNGLSHADERSOURCEPROC glShaderSource;
|
||||
PFNGLCOMPILESHADERPROC glCompileShader;
|
||||
PFNGLGETSHADERIVPROC glGetShaderiv;
|
||||
PFNGLGETSHADERINFOLOGPROC glGetShaderInfoLog;
|
||||
PFNGLDELETESHADERPROC glDeleteShader;
|
||||
PFNGLATTACHSHADERPROC glAttachShader;
|
||||
PFNGLCREATEPROGRAMPROC glCreateProgram;
|
||||
PFNGLLINKPROGRAMPROC glLinkProgram;
|
||||
PFNGLVALIDATEPROGRAMPROC glValidateProgram;
|
||||
PFNGLGETPROGRAMIVPROC glGetProgramiv;
|
||||
PFNGLGETPROGRAMINFOLOGPROC glGetProgramInfoLog;
|
||||
PFNGLUSEPROGRAMPROC glUseProgram;
|
||||
|
||||
bool initGLExtensions()
|
||||
{
|
||||
glCreateShader = (PFNGLCREATESHADERPROC)SDL_GL_GetProcAddress("glCreateShader");
|
||||
glShaderSource = (PFNGLSHADERSOURCEPROC)SDL_GL_GetProcAddress("glShaderSource");
|
||||
glCompileShader = (PFNGLCOMPILESHADERPROC)SDL_GL_GetProcAddress("glCompileShader");
|
||||
glGetShaderiv = (PFNGLGETSHADERIVPROC)SDL_GL_GetProcAddress("glGetShaderiv");
|
||||
glGetShaderInfoLog = (PFNGLGETSHADERINFOLOGPROC)SDL_GL_GetProcAddress("glGetShaderInfoLog");
|
||||
glDeleteShader = (PFNGLDELETESHADERPROC)SDL_GL_GetProcAddress("glDeleteShader");
|
||||
glAttachShader = (PFNGLATTACHSHADERPROC)SDL_GL_GetProcAddress("glAttachShader");
|
||||
glCreateProgram = (PFNGLCREATEPROGRAMPROC)SDL_GL_GetProcAddress("glCreateProgram");
|
||||
glLinkProgram = (PFNGLLINKPROGRAMPROC)SDL_GL_GetProcAddress("glLinkProgram");
|
||||
glValidateProgram = (PFNGLVALIDATEPROGRAMPROC)SDL_GL_GetProcAddress("glValidateProgram");
|
||||
glGetProgramiv = (PFNGLGETPROGRAMIVPROC)SDL_GL_GetProcAddress("glGetProgramiv");
|
||||
glGetProgramInfoLog = (PFNGLGETPROGRAMINFOLOGPROC)SDL_GL_GetProcAddress("glGetProgramInfoLog");
|
||||
glUseProgram = (PFNGLUSEPROGRAMPROC)SDL_GL_GetProcAddress("glUseProgram");
|
||||
|
||||
return glCreateShader && glShaderSource && glCompileShader && glGetShaderiv &&
|
||||
glGetShaderInfoLog && glDeleteShader && glAttachShader && glCreateProgram &&
|
||||
glLinkProgram && glValidateProgram && glGetProgramiv && glGetProgramInfoLog &&
|
||||
glUseProgram;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
GLuint compileShader(const char *source, GLuint shaderType)
|
||||
{
|
||||
// Create ID for shader
|
||||
GLuint result = glCreateShader(shaderType);
|
||||
// Add define depending on shader type
|
||||
const char *sources[2] = {shaderType == GL_VERTEX_SHADER ? "#define VERTEX\n" : "#define FRAGMENT\n", source};
|
||||
// Define shader text
|
||||
glShaderSource(result, 2, sources, NULL);
|
||||
// Compile shader
|
||||
glCompileShader(result);
|
||||
|
||||
// Check vertex shader for errors
|
||||
GLint shaderCompiled = GL_FALSE;
|
||||
glGetShaderiv(result, GL_COMPILE_STATUS, &shaderCompiled);
|
||||
if (shaderCompiled != GL_TRUE)
|
||||
{
|
||||
std::cout << "Error en la compilación: " << result << "!" << std::endl;
|
||||
GLint logLength;
|
||||
glGetShaderiv(result, GL_INFO_LOG_LENGTH, &logLength);
|
||||
if (logLength > 0)
|
||||
{
|
||||
GLchar *log = (GLchar *)malloc(logLength);
|
||||
glGetShaderInfoLog(result, logLength, &logLength, log);
|
||||
std::cout << "Shader compile log:" << log << std::endl;
|
||||
free(log);
|
||||
}
|
||||
glDeleteShader(result);
|
||||
result = 0;
|
||||
// } else {
|
||||
// std::cout << "Shader compilado correctamente. Id = " << result << std::endl;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
GLuint compileProgram(const char *vertexShaderSource, const char *fragmentShaderSource)
|
||||
{
|
||||
GLuint programId = 0;
|
||||
GLuint vtxShaderId, fragShaderId;
|
||||
|
||||
programId = glCreateProgram();
|
||||
|
||||
vtxShaderId = compileShader(vertexShaderSource, GL_VERTEX_SHADER);
|
||||
fragShaderId = compileShader(fragmentShaderSource ? fragmentShaderSource : vertexShaderSource, GL_FRAGMENT_SHADER);
|
||||
|
||||
if (vtxShaderId && fragShaderId)
|
||||
{
|
||||
// Associate shader with program
|
||||
glAttachShader(programId, vtxShaderId);
|
||||
glAttachShader(programId, fragShaderId);
|
||||
glLinkProgram(programId);
|
||||
glValidateProgram(programId);
|
||||
|
||||
// Check the status of the compile/link
|
||||
GLint logLen;
|
||||
glGetProgramiv(programId, GL_INFO_LOG_LENGTH, &logLen);
|
||||
if (logLen > 0)
|
||||
{
|
||||
char *log = (char *)malloc(logLen * sizeof(char));
|
||||
// Show any errors as appropriate
|
||||
glGetProgramInfoLog(programId, logLen, &logLen, log);
|
||||
std::cout << "Prog Info Log: " << std::endl
|
||||
<< log << std::endl;
|
||||
free(log);
|
||||
}
|
||||
}
|
||||
if (vtxShaderId)
|
||||
{
|
||||
glDeleteShader(vtxShaderId);
|
||||
}
|
||||
if (fragShaderId)
|
||||
{
|
||||
glDeleteShader(fragShaderId);
|
||||
}
|
||||
return programId;
|
||||
}
|
||||
|
||||
const bool init(SDL_Window *win, SDL_Texture *backBuffer, const char *vertexShader, const char *fragmentShader)
|
||||
{
|
||||
shader::win = win;
|
||||
shader::renderer = SDL_GetRenderer(win);
|
||||
shader::backBuffer = backBuffer;
|
||||
SDL_GetWindowSize(win, &win_size.x, &win_size.y);
|
||||
int access;
|
||||
SDL_QueryTexture(backBuffer, NULL, &access, &tex_size.x, &tex_size.y);
|
||||
if (access != SDL_TEXTUREACCESS_TARGET)
|
||||
{
|
||||
std::cout << "ERROR FATAL: La textura per al render ha de tindre SDL_TEXTUREACCESS_TARGET definit." << std::endl;
|
||||
exit(1);
|
||||
}
|
||||
|
||||
SDL_RendererInfo rendererInfo;
|
||||
SDL_GetRendererInfo(renderer, &rendererInfo);
|
||||
|
||||
if (!strncmp(rendererInfo.name, "opengl", 6))
|
||||
{
|
||||
// std::cout << "Es OpenGL!" << std::endl;
|
||||
#ifndef __APPLE__
|
||||
if (!initGLExtensions())
|
||||
{
|
||||
std::cout << "WARNING: No s'han pogut inicialitzar les extensions d'OpenGL!" << std::endl;
|
||||
usingOpenGL = false;
|
||||
return false;
|
||||
}
|
||||
#endif
|
||||
// Compilar el shader y dejarlo listo para usar.
|
||||
programId = compileProgram(vertexShader, fragmentShader);
|
||||
// std::cout << "programId = " << programId << std::endl;
|
||||
}
|
||||
else
|
||||
{
|
||||
std::cout << "WARNING: El driver del renderer no es OpenGL." << std::endl;
|
||||
usingOpenGL = false;
|
||||
return false;
|
||||
}
|
||||
usingOpenGL = true;
|
||||
return true;
|
||||
}
|
||||
|
||||
void render()
|
||||
{
|
||||
GLint oldProgramId;
|
||||
// Guarrada para obtener el textureid (en driverdata->texture)
|
||||
// Detach the texture
|
||||
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
|
||||
|
||||
SDL_SetRenderTarget(renderer, NULL);
|
||||
SDL_RenderClear(renderer);
|
||||
|
||||
if (usingOpenGL)
|
||||
{
|
||||
SDL_GL_BindTexture(backBuffer, NULL, NULL);
|
||||
if (programId != 0)
|
||||
{
|
||||
glGetIntegerv(GL_CURRENT_PROGRAM, &oldProgramId);
|
||||
glUseProgram(programId);
|
||||
}
|
||||
|
||||
GLfloat minx, miny, maxx, maxy;
|
||||
GLfloat minu, maxu, minv, maxv;
|
||||
|
||||
// Coordenadas de la ventana donde pintar.
|
||||
minx = 0.0f;
|
||||
miny = 0.0f;
|
||||
maxx = tex_size.x;
|
||||
maxy = tex_size.y;
|
||||
|
||||
minu = 0.0f;
|
||||
maxu = 1.0f;
|
||||
minv = 0.0f;
|
||||
maxv = 1.0f;
|
||||
|
||||
glViewport(0, 0, win_size.x, win_size.y);
|
||||
|
||||
glBegin(GL_TRIANGLE_STRIP);
|
||||
glTexCoord2f(minu, minv);
|
||||
glVertex2f(minx, miny);
|
||||
glTexCoord2f(maxu, minv);
|
||||
glVertex2f(maxx, miny);
|
||||
glTexCoord2f(minu, maxv);
|
||||
glVertex2f(minx, maxy);
|
||||
glTexCoord2f(maxu, maxv);
|
||||
glVertex2f(maxx, maxy);
|
||||
glEnd();
|
||||
SDL_GL_SwapWindow(win);
|
||||
|
||||
if (programId != 0)
|
||||
{
|
||||
glUseProgram(oldProgramId);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
SDL_RenderCopy(renderer, backBuffer, NULL, NULL);
|
||||
SDL_RenderPresent(renderer);
|
||||
}
|
||||
}
|
||||
}
|
||||
45
source/jail_shader.h
Normal file
45
source/jail_shader.h
Normal file
@@ -0,0 +1,45 @@
|
||||
#pragma once
|
||||
|
||||
#include <SDL2/SDL_render.h> // para SDL_Texture
|
||||
#include <SDL2/SDL_video.h> // para SDL_Window
|
||||
|
||||
// TIPS:
|
||||
// =======================================================================
|
||||
// Abans de crear el renderer, cridar a la següent funció:
|
||||
//
|
||||
// SDL_SetHint(SDL_HINT_RENDER_DRIVER, "opengl");
|
||||
//
|
||||
// Aixó li diu que volem un renderer que use especificament opengl. A més,
|
||||
// al crear el renderer li tenim que dir que el volem que use acceeració
|
||||
// per hardware, i que soporte render a textura. Per exemple:
|
||||
//
|
||||
// SDL_Renderer *ren = SDL_CreateRenderer(win, -1, SDL_RENDERER_ACCELERATED |
|
||||
// SDL_RENDERER_TARGETTEXTURE);
|
||||
//
|
||||
// Per altra part, al crear la textura tenim que definir que puga ser target
|
||||
// de renderitzat (SDL_TEXTUREACCESS_TARGET), per exemple:
|
||||
//
|
||||
// SDL_Texture *tex = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_ARGB8888,
|
||||
// SDL_TEXTUREACCESS_TARGET, 320, 240);
|
||||
//
|
||||
// Els shaders li'ls passem com una cadena, som nosaltres els que s'encarreguem
|
||||
// de carregarlos de disc, amb fopen, ifstream, jfile o el que vullgues.
|
||||
// Si els tens en un std::string, passa-li-la com "cadena.c_str()".
|
||||
//
|
||||
// Poden ser els dos el mateix arxiu, com fa libRetro, jo desde dins ja fique
|
||||
// els defines necessaris. Si es el mateix arxiu, pots no ficar el quart paràmetre.
|
||||
//
|
||||
// Els shaders de libRetro no funcionen directament, hi ha que fer algunes modificacions.
|
||||
//
|
||||
// El pintat final de la teua escena l'has de fer com si "backBuffer" fora la pantalla.
|
||||
//
|
||||
// Ah! una cosa mes: al compilar, en Linux afegir "-lGL", en Windows afegir "-lopengl32".
|
||||
// En Mac ni idea
|
||||
|
||||
namespace shader
|
||||
{
|
||||
const bool init(SDL_Window *win, SDL_Texture *backBuffer,
|
||||
const char *vertexShader, const char *fragmentShader = nullptr);
|
||||
|
||||
void render();
|
||||
}
|
||||
356
source/loading_screen.cpp
Normal file
356
source/loading_screen.cpp
Normal file
@@ -0,0 +1,356 @@
|
||||
#include "loading_screen.h"
|
||||
#include <SDL2/SDL_timer.h> // for SDL_GetTicks
|
||||
#include <stdlib.h> // for rand
|
||||
#include "asset.h" // for Asset
|
||||
#include "const.h" // for SECTION_LOADING_SCREEN, SECTION_QUIT
|
||||
#include "input.h" // for Input, REPEAT_FALSE, inputs_e
|
||||
#include "jail_audio.h" // for JA_DeleteMusic, JA_LoadMusic, JA_PlayMusic
|
||||
#include "resource.h" // for Resource
|
||||
#include "screen.h" // for Screen
|
||||
#include "sprite.h" // for Sprite
|
||||
#include "texture.h" // for Texture
|
||||
#include "utils.h" // for options_t, section_t, color_t, stringToC...
|
||||
#include "options.h"
|
||||
|
||||
// Constructor
|
||||
LoadingScreen::LoadingScreen()
|
||||
: screen_(Screen::get()),
|
||||
renderer_(Screen::get()->getRenderer()),
|
||||
resource_(Resource::get()),
|
||||
asset_(Asset::get()),
|
||||
input_(Input::get())
|
||||
{
|
||||
// Reserva memoria para los punteros
|
||||
if (options.palette == p_zxspectrum)
|
||||
{
|
||||
mono_loading_screen_texture_ = resource_->getTexture("loading_screen_bn.png");
|
||||
color_loading_screen_texture_ = resource_->getTexture("loading_screen_color.png");
|
||||
}
|
||||
else if (options.palette == p_zxarne)
|
||||
{
|
||||
mono_loading_screen_texture_ = resource_->getTexture("loading_screen_bn_zxarne.png");
|
||||
color_loading_screen_texture_ = resource_->getTexture("loading_screen_color_zxarne.png");
|
||||
}
|
||||
mono_loading_screen_sprite_ = new Sprite(0, 0, mono_loading_screen_texture_->getWidth(), mono_loading_screen_texture_->getHeight(), mono_loading_screen_texture_, renderer_);
|
||||
color_loading_screen_sprite_ = new Sprite(0, 0, color_loading_screen_texture_->getWidth(), color_loading_screen_texture_->getHeight(), color_loading_screen_texture_, renderer_);
|
||||
loading_sound1_ = JA_LoadMusic(asset_->get("loading_sound1.ogg").c_str());
|
||||
loading_sound2_ = JA_LoadMusic(asset_->get("loading_sound2.ogg").c_str());
|
||||
loading_sound3_ = JA_LoadMusic(asset_->get("loading_sound3.ogg").c_str());
|
||||
|
||||
// Inicializa variables
|
||||
options.section.name = SECTION_LOADING_SCREEN;
|
||||
options.section.subsection = 0;
|
||||
|
||||
// Establece el orden de las lineas para imitar el direccionamiento de memoria del spectrum
|
||||
for (int i = 0; i < 192; ++i)
|
||||
{
|
||||
if (i < 64)
|
||||
{ // Primer bloque de 2K
|
||||
line_index_[i] = ((i % 8) * 8) + (i / 8);
|
||||
}
|
||||
|
||||
else if (i >= 64 && i < 128)
|
||||
{ // Segundo bloque de 2K
|
||||
line_index_[i] = 64 + ((i % 8) * 8) + ((i - 64) / 8);
|
||||
}
|
||||
|
||||
else if (i >= 128 && i < 192)
|
||||
{ // tercer bloque de 2K
|
||||
line_index_[i] = 128 + ((i % 8) * 8) + ((i - 128) / 8);
|
||||
}
|
||||
}
|
||||
|
||||
// Cambia el color del borde
|
||||
screen_->setBorderColor(stringToColor(options.palette, "black"));
|
||||
}
|
||||
|
||||
// Destructor
|
||||
LoadingScreen::~LoadingScreen()
|
||||
{
|
||||
delete mono_loading_screen_sprite_;
|
||||
delete color_loading_screen_sprite_;
|
||||
JA_DeleteMusic(loading_sound1_);
|
||||
JA_DeleteMusic(loading_sound2_);
|
||||
JA_DeleteMusic(loading_sound3_);
|
||||
}
|
||||
|
||||
// Comprueba el manejador de eventos
|
||||
void LoadingScreen::checkEvents()
|
||||
{
|
||||
SDL_Event event;
|
||||
while (SDL_PollEvent(&event))
|
||||
{
|
||||
// Evento de salida de la aplicación
|
||||
if (event.type == SDL_QUIT)
|
||||
{
|
||||
options.section.name = SECTION_QUIT;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Comprueba las entradas
|
||||
void LoadingScreen::checkInput()
|
||||
{
|
||||
|
||||
if (input_->checkInput(input_exit, REPEAT_FALSE))
|
||||
{
|
||||
options.section.name = SECTION_QUIT;
|
||||
}
|
||||
|
||||
else if (input_->checkInput(input_toggle_border, REPEAT_FALSE))
|
||||
{
|
||||
screen_->toggleBorder();
|
||||
}
|
||||
|
||||
else if (input_->checkInput(input_toggle_videomode, REPEAT_FALSE))
|
||||
{
|
||||
screen_->toggleVideoMode();
|
||||
}
|
||||
|
||||
else if (input_->checkInput(input_window_dec_size, REPEAT_FALSE))
|
||||
{
|
||||
screen_->decWindowSize();
|
||||
}
|
||||
|
||||
else if (input_->checkInput(input_window_inc_size, REPEAT_FALSE))
|
||||
{
|
||||
screen_->incWindowSize();
|
||||
}
|
||||
|
||||
else if (input_->checkInput(input_toggle_palette, REPEAT_FALSE))
|
||||
{
|
||||
switchPalette();
|
||||
}
|
||||
|
||||
else if (input_->checkInput(input_pause, REPEAT_FALSE) || input_->checkInput(input_accept, REPEAT_FALSE) || input_->checkInput(input_jump, REPEAT_FALSE))
|
||||
{
|
||||
options.section.name = SECTION_TITLE;
|
||||
options.section.subsection = 0;
|
||||
}
|
||||
}
|
||||
|
||||
// Gestiona el contador de carga
|
||||
void LoadingScreen::updateLoad()
|
||||
{
|
||||
// Primera parte de la carga, la parte en blanco y negro
|
||||
if (loading_first_part_)
|
||||
{
|
||||
// Cada 5 pasos el loadCounter se incrementa en uno
|
||||
const int numSteps = 5;
|
||||
const int step = 51;
|
||||
load_counter_ = counter_ / numSteps;
|
||||
|
||||
if (load_counter_ < 192)
|
||||
{
|
||||
load_rect_.x = step * (counter_ % numSteps);
|
||||
load_rect_.y = line_index_[load_counter_];
|
||||
mono_loading_screen_sprite_->setSpriteClip(load_rect_);
|
||||
mono_loading_screen_sprite_->setRect(load_rect_);
|
||||
}
|
||||
// Una vez actualizadas las 192 lineas, pasa a la segunda fase de la carga
|
||||
else if (load_counter_ == 192)
|
||||
{
|
||||
loading_first_part_ = false;
|
||||
load_counter_ = 0;
|
||||
load_rect_ = {0, 0, 16, 8};
|
||||
color_loading_screen_sprite_->setRect(load_rect_);
|
||||
color_loading_screen_sprite_->setSpriteClip(load_rect_);
|
||||
JA_PlayMusic(loading_sound3_);
|
||||
}
|
||||
}
|
||||
// Segunda parte de la carga, la parte de los bloques en color
|
||||
else
|
||||
{
|
||||
load_counter_ += 2;
|
||||
load_rect_.x = (load_counter_ * 8) % 256;
|
||||
load_rect_.y = (load_counter_ / 32) * 8;
|
||||
color_loading_screen_sprite_->setSpriteClip(load_rect_);
|
||||
color_loading_screen_sprite_->setRect(load_rect_);
|
||||
|
||||
// Comprueba si ha terminado la intro
|
||||
if (load_counter_ >= 768)
|
||||
{
|
||||
options.section.name = SECTION_TITLE;
|
||||
options.section.subsection = SUBSECTION_TITLE_WITH_LOADING_SCREEN;
|
||||
JA_StopMusic();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Gestiona el contador interno
|
||||
void LoadingScreen::updateCounter()
|
||||
{
|
||||
(pre_counter_ >= 50) ? counter_++ : pre_counter_++;
|
||||
|
||||
if (counter_ == 1)
|
||||
{
|
||||
JA_PlayMusic(loading_sound2_);
|
||||
}
|
||||
}
|
||||
|
||||
// Dibuja la pantalla de carga
|
||||
void LoadingScreen::renderLoad()
|
||||
{
|
||||
loading_first_part_ ? mono_loading_screen_sprite_->render() : color_loading_screen_sprite_->render();
|
||||
}
|
||||
|
||||
// Dibuja el efecto de carga en el borde
|
||||
void LoadingScreen::renderBorder()
|
||||
{
|
||||
// Pinta el borde de colro azul
|
||||
color_t color = stringToColor(options.palette, "blue");
|
||||
SDL_SetRenderDrawColor(renderer_, color.r, color.g, color.b, 0xFF);
|
||||
SDL_RenderClear(renderer_);
|
||||
|
||||
// Añade lineas amarillas
|
||||
color = stringToColor(options.palette, "yellow");
|
||||
SDL_SetRenderDrawColor(renderer_, color.r, color.g, color.b, 0xFF);
|
||||
const int width = GAMECANVAS_WIDTH + (options.borderWidth * 2);
|
||||
const int height = GAMECANVAS_HEIGHT + (options.borderHeight * 2);
|
||||
bool drawEnabled = rand() % 2 == 0 ? true : false;
|
||||
// Para (int i = 0; i < height; ++i)
|
||||
//{
|
||||
// if (rand() % 2 == 0)
|
||||
// {
|
||||
// SDL_RenderDrawLine(renderer, 0, i, width, i);
|
||||
// }
|
||||
// }
|
||||
int row = 0;
|
||||
int rowSize = 1;
|
||||
while (row < height)
|
||||
{
|
||||
rowSize = (rand() % 4) + 3;
|
||||
if (drawEnabled)
|
||||
for (int i = row; i < row + rowSize; ++i)
|
||||
{
|
||||
SDL_RenderDrawLine(renderer_, 0, i, width, i);
|
||||
}
|
||||
row += rowSize;
|
||||
drawEnabled = !drawEnabled;
|
||||
}
|
||||
}
|
||||
|
||||
// Actualiza las variables
|
||||
void LoadingScreen::update()
|
||||
{
|
||||
// Comprueba que la diferencia de ticks sea mayor a la velocidad del juego
|
||||
if (SDL_GetTicks() - ticks_ > ticks_speed_)
|
||||
{
|
||||
// Actualiza el contador de ticks
|
||||
ticks_ = SDL_GetTicks();
|
||||
|
||||
// Comprueba las entradas
|
||||
checkInput();
|
||||
|
||||
// Gestiona el contador interno
|
||||
updateCounter();
|
||||
|
||||
// Gestiona el contador de carga
|
||||
updateLoad();
|
||||
|
||||
screen_->update();
|
||||
}
|
||||
}
|
||||
|
||||
// Dibuja en pantalla
|
||||
void LoadingScreen::render()
|
||||
{
|
||||
if (options.borderEnabled)
|
||||
{
|
||||
// Prepara para empezar a dibujar en la textura del borde
|
||||
screen_->startDrawOnBorder();
|
||||
|
||||
// Dibuja el efecto de carga en el borde
|
||||
renderBorder();
|
||||
}
|
||||
|
||||
// Prepara para empezar a dibujar en la textura de juego
|
||||
screen_->start();
|
||||
|
||||
// Dibuja la pantalla de carga
|
||||
renderLoad();
|
||||
|
||||
// Vuelca el contenido del renderizador en pantalla
|
||||
screen_->render();
|
||||
}
|
||||
|
||||
// Bucle para el logo del juego
|
||||
void LoadingScreen::run()
|
||||
{
|
||||
// Inicia el sonido de carga
|
||||
JA_SetVolume(64);
|
||||
JA_PlayMusic(loading_sound1_);
|
||||
|
||||
// Limpia la pantalla
|
||||
screen_->start();
|
||||
screen_->clean();
|
||||
screen_->render();
|
||||
|
||||
while (options.section.name == SECTION_LOADING_SCREEN)
|
||||
{
|
||||
update();
|
||||
checkEvents();
|
||||
render();
|
||||
}
|
||||
|
||||
JA_SetVolume(128);
|
||||
}
|
||||
|
||||
// Cambia la paleta
|
||||
void LoadingScreen::switchPalette()
|
||||
{
|
||||
if (options.palette == p_zxspectrum)
|
||||
{
|
||||
options.palette = p_zxarne;
|
||||
mono_loading_screen_sprite_->setTexture(resource_->getTexture("loading_screen_bn_zxarne.png"));
|
||||
color_loading_screen_sprite_->setTexture(resource_->getTexture("loading_screen_color_zxarne.png"));
|
||||
}
|
||||
else
|
||||
{
|
||||
options.palette = p_zxspectrum;
|
||||
mono_loading_screen_sprite_->setTexture(resource_->getTexture("loading_screen_bn.png"));
|
||||
color_loading_screen_sprite_->setTexture(resource_->getTexture("loading_screen_color.png"));
|
||||
}
|
||||
|
||||
recreateLoadingScreen();
|
||||
}
|
||||
|
||||
// Reconstruye la pantalla de carga
|
||||
void LoadingScreen::recreateLoadingScreen()
|
||||
{
|
||||
// Prepara para empezar a dibujar en la textura de juego
|
||||
screen_->start();
|
||||
|
||||
// Primera parte de la carga, la parte en blanco y negro
|
||||
if (loading_first_part_)
|
||||
{
|
||||
const int numSteps = 5;
|
||||
const int step = 51;
|
||||
|
||||
for (int i = 0; i <= counter_; i++)
|
||||
{
|
||||
load_counter_ = i / numSteps;
|
||||
load_rect_.x = step * (i % numSteps);
|
||||
load_rect_.y = line_index_[load_counter_];
|
||||
mono_loading_screen_sprite_->setSpriteClip(load_rect_);
|
||||
mono_loading_screen_sprite_->setRect(load_rect_);
|
||||
mono_loading_screen_sprite_->render();
|
||||
}
|
||||
}
|
||||
// Segunda parte de la carga, la parte de los bloques en color
|
||||
else
|
||||
{
|
||||
for (int i = 0; i <= load_counter_; i++)
|
||||
{
|
||||
load_rect_.x = (i * 8) % 256;
|
||||
load_rect_.y = (i / 32) * 8;
|
||||
color_loading_screen_sprite_->setSpriteClip(load_rect_);
|
||||
color_loading_screen_sprite_->setRect(load_rect_);
|
||||
color_loading_screen_sprite_->render();
|
||||
}
|
||||
}
|
||||
|
||||
// Vuelca el contenido del renderizador en pantalla
|
||||
screen_->render();
|
||||
}
|
||||
83
source/loading_screen.h
Normal file
83
source/loading_screen.h
Normal file
@@ -0,0 +1,83 @@
|
||||
#pragma once
|
||||
|
||||
#include <SDL2/SDL_events.h> // Para SDL_Event
|
||||
#include <SDL2/SDL_rect.h> // Para SDL_Rect
|
||||
#include <SDL2/SDL_render.h> // Para SDL_Renderer
|
||||
#include <SDL2/SDL_stdinc.h> // Para Uint32
|
||||
class Asset;
|
||||
class Input;
|
||||
class Resource;
|
||||
class Screen;
|
||||
class Sprite;
|
||||
class Texture;
|
||||
struct JA_Music_t;
|
||||
struct options_t;
|
||||
struct section_t;
|
||||
|
||||
class LoadingScreen
|
||||
{
|
||||
private:
|
||||
// Objetos y punteros
|
||||
Screen *screen_; // Objeto encargado de dibujar en pantalla
|
||||
SDL_Renderer *renderer_; // El renderizador de la ventana
|
||||
Resource *resource_; // Objeto con los recursos
|
||||
Asset *asset_; // Objeto con los ficheros de recursos
|
||||
Input *input_; // Objeto pata gestionar la entrada
|
||||
Texture *mono_loading_screen_texture_; // Textura con la pantalla de carga en blanco y negro
|
||||
Texture *color_loading_screen_texture_; // Textura con la pantalla de carga en color
|
||||
Sprite *mono_loading_screen_sprite_; // Sprite para manejar la textura loadingScreenTexture1
|
||||
Sprite *color_loading_screen_sprite_; // Sprite para manejar la textura loadingScreenTexture2
|
||||
|
||||
// Variables
|
||||
int pre_counter_ = 0; // Contador previo para realizar una pausa inicial
|
||||
int counter_ = 0; // Contador
|
||||
Uint32 ticks_ = 0; // Contador de ticks para ajustar la velocidad del programa
|
||||
Uint32 ticks_speed_ = 15; // Velocidad a la que se repiten los bucles del programa
|
||||
int load_counter_ = 0; // Contador para controlar las cargas
|
||||
bool loading_first_part_ = true; // Para saber en que parte de la carga se encuentra
|
||||
JA_Music_t *loading_sound1_; // Sonidos para imitar la carga tipo spectrum
|
||||
JA_Music_t *loading_sound2_; // Sonidos para imitar la carga tipo spectrum
|
||||
JA_Music_t *loading_sound3_; // Sonidos para imitar la carga tipo spectrum
|
||||
int line_index_[192]; // El orden en el que se procesan las 192 lineas de la pantalla de carga
|
||||
SDL_Rect load_rect_ = {0, 0, 51, 1}; // Rectangulo para dibujar la pantalla de carga
|
||||
|
||||
// Actualiza las variables
|
||||
void update();
|
||||
|
||||
// Dibuja en pantalla
|
||||
void render();
|
||||
|
||||
// Comprueba el manejador de eventos
|
||||
void checkEvents();
|
||||
|
||||
// Comprueba las entradas
|
||||
void checkInput();
|
||||
|
||||
// Gestiona el contador interno
|
||||
void updateCounter();
|
||||
|
||||
// Gestiona el contador de carga
|
||||
void updateLoad();
|
||||
|
||||
// Dibuja la pantalla de carga
|
||||
void renderLoad();
|
||||
|
||||
// Dibuja el efecto de carga en el borde
|
||||
void renderBorder();
|
||||
|
||||
// Cambia la paleta
|
||||
void switchPalette();
|
||||
|
||||
// Reconstruye la pantalla de carga
|
||||
void recreateLoadingScreen();
|
||||
|
||||
public:
|
||||
// Constructor
|
||||
LoadingScreen();
|
||||
|
||||
// Destructor
|
||||
~LoadingScreen();
|
||||
|
||||
// Bucle principal
|
||||
void run();
|
||||
};
|
||||
331
source/logo.cpp
Normal file
331
source/logo.cpp
Normal file
@@ -0,0 +1,331 @@
|
||||
#include "logo.h"
|
||||
#include <SDL2/SDL_timer.h> // for SDL_GetTicks
|
||||
#include <string> // for basic_string, string
|
||||
#include "const.h" // for SECTION_LOGO, SECTION_TITLE, SUBSECTION_...
|
||||
#include "input.h" // for Input, REPEAT_FALSE, inputs_e
|
||||
#include "jail_audio.h" // for JA_StopMusic
|
||||
#include "resource.h" // for Resource
|
||||
#include "screen.h" // for Screen
|
||||
#include "sprite.h" // for Sprite
|
||||
#include "texture.h" // for Texture
|
||||
#include "utils.h" // for color_t, section_t, options_t, stringToC...
|
||||
#include "asset.h"
|
||||
#include "options.h"
|
||||
class Asset; // lines 11-11
|
||||
|
||||
// Constructor
|
||||
Logo::Logo()
|
||||
: screen_(Screen::get()),
|
||||
renderer_(Screen::get()->getRenderer()),
|
||||
resource_(Resource::get()),
|
||||
asset_(Asset::get()),
|
||||
input_(Input::get())
|
||||
{
|
||||
// Reserva memoria para los punteros
|
||||
jailgames_texture_ = resource_->getTexture("jailgames.png");
|
||||
since_1998_texture_ = resource_->getTexture("since_1998.png");
|
||||
since_1998_sprite_ = new Sprite((256 - since_1998_texture_->getWidth()) / 2, 83 + jailgames_texture_->getHeight() + 5, since_1998_texture_->getWidth(), since_1998_texture_->getHeight(), since_1998_texture_, renderer_);
|
||||
since_1998_sprite_->setSpriteClip(0, 0, since_1998_texture_->getWidth(), since_1998_texture_->getHeight());
|
||||
since_1998_texture_->setColor(0, 0, 0);
|
||||
|
||||
// Crea los sprites de cada linea
|
||||
for (int i = 0; i < jailgames_texture_->getHeight(); ++i)
|
||||
{
|
||||
jailgames_sprite_.push_back(new Sprite(0, i, jailgames_texture_->getWidth(), 1, jailgames_texture_, renderer_));
|
||||
jailgames_sprite_.back()->setSpriteClip(0, i, jailgames_texture_->getWidth(), 1);
|
||||
if (i % 2 == 0)
|
||||
{
|
||||
jailgames_sprite_[i]->setPosX(256 + (i * 3));
|
||||
}
|
||||
else
|
||||
{
|
||||
jailgames_sprite_[i]->setPosX(-181 - (i * 3));
|
||||
}
|
||||
jailgames_sprite_[i]->setPosY(83 + i);
|
||||
}
|
||||
|
||||
// Inicializa variables
|
||||
options.section.name = SECTION_LOGO;
|
||||
|
||||
// Inicializa el vector de colores
|
||||
const std::vector<std::string> vColors = {"black", "blue", "red", "magenta", "green", "cyan", "yellow", "bright_white"};
|
||||
for (auto v : vColors)
|
||||
{
|
||||
color_.push_back(stringToColor(options.palette, v));
|
||||
}
|
||||
|
||||
// Cambia el color del borde
|
||||
screen_->setBorderColor(stringToColor(options.palette, "black"));
|
||||
}
|
||||
|
||||
// Destructor
|
||||
Logo::~Logo()
|
||||
{
|
||||
for (auto s : jailgames_sprite_)
|
||||
{
|
||||
delete s;
|
||||
}
|
||||
|
||||
delete since_1998_sprite_;
|
||||
}
|
||||
|
||||
// Comprueba el manejador de eventos
|
||||
void Logo::checkEvents()
|
||||
{
|
||||
SDL_Event event;
|
||||
while (SDL_PollEvent(&event))
|
||||
{
|
||||
// Evento de salida de la aplicación
|
||||
if (event.type == SDL_QUIT)
|
||||
{
|
||||
options.section.name = SECTION_QUIT;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Comprueba las entradas
|
||||
void Logo::checkInput()
|
||||
{
|
||||
if (input_->checkInput(input_exit, REPEAT_FALSE))
|
||||
{
|
||||
options.section.name = SECTION_TITLE;
|
||||
}
|
||||
|
||||
else if (input_->checkInput(input_toggle_border, REPEAT_FALSE))
|
||||
{
|
||||
screen_->toggleBorder();
|
||||
}
|
||||
|
||||
else if (input_->checkInput(input_toggle_videomode, REPEAT_FALSE))
|
||||
{
|
||||
screen_->toggleVideoMode();
|
||||
}
|
||||
|
||||
else if (input_->checkInput(input_window_dec_size, REPEAT_FALSE))
|
||||
{
|
||||
screen_->decWindowSize();
|
||||
}
|
||||
|
||||
else if (input_->checkInput(input_window_inc_size, REPEAT_FALSE))
|
||||
{
|
||||
screen_->incWindowSize();
|
||||
}
|
||||
|
||||
else if (input_->checkInput(input_toggle_palette, REPEAT_FALSE))
|
||||
{
|
||||
switchPalette();
|
||||
}
|
||||
|
||||
else if (input_->checkInput(input_pause, REPEAT_FALSE) || input_->checkInput(input_accept, REPEAT_FALSE) || input_->checkInput(input_jump, REPEAT_FALSE))
|
||||
{
|
||||
options.section.subsection = SUBSECTION_LOGO_TO_TITLE;
|
||||
endSection();
|
||||
}
|
||||
}
|
||||
|
||||
// Gestiona el logo de JAILGAME
|
||||
void Logo::updateJAILGAMES()
|
||||
{
|
||||
if (counter_ > 30)
|
||||
{
|
||||
for (int i = 1; i < (int)jailgames_sprite_.size(); ++i)
|
||||
{
|
||||
const int speed = 8;
|
||||
const int dest = 37;
|
||||
if (jailgames_sprite_[i]->getPosX() != 37)
|
||||
{
|
||||
if (i % 2 == 0)
|
||||
{
|
||||
jailgames_sprite_[i]->incPosX(-speed);
|
||||
if (jailgames_sprite_[i]->getPosX() < dest)
|
||||
{
|
||||
jailgames_sprite_[i]->setPosX(dest);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
jailgames_sprite_[i]->incPosX(speed);
|
||||
if (jailgames_sprite_[i]->getPosX() > dest)
|
||||
{
|
||||
jailgames_sprite_[i]->setPosX(dest);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Gestiona el color de las texturas
|
||||
void Logo::updateTextureColors()
|
||||
{
|
||||
const int ini = 70;
|
||||
const int inc = 4;
|
||||
|
||||
if (counter_ == ini + inc * 0)
|
||||
{
|
||||
since_1998_texture_->setColor(color_[0].r, color_[0].g, color_[0].b);
|
||||
}
|
||||
|
||||
else if (counter_ == ini + inc * 1)
|
||||
{
|
||||
since_1998_texture_->setColor(color_[1].r, color_[1].g, color_[1].b);
|
||||
}
|
||||
|
||||
else if (counter_ == ini + inc * 2)
|
||||
{
|
||||
since_1998_texture_->setColor(color_[2].r, color_[2].g, color_[2].b);
|
||||
}
|
||||
|
||||
else if (counter_ == ini + inc * 3)
|
||||
{
|
||||
since_1998_texture_->setColor(color_[3].r, color_[3].g, color_[3].b);
|
||||
}
|
||||
|
||||
else if (counter_ == ini + inc * 4)
|
||||
{
|
||||
since_1998_texture_->setColor(color_[4].r, color_[4].g, color_[4].b);
|
||||
}
|
||||
|
||||
else if (counter_ == ini + inc * 5)
|
||||
{
|
||||
since_1998_texture_->setColor(color_[5].r, color_[5].g, color_[5].b);
|
||||
}
|
||||
|
||||
else if (counter_ == ini + inc * 6)
|
||||
{
|
||||
since_1998_texture_->setColor(color_[6].r, color_[6].g, color_[6].b);
|
||||
}
|
||||
|
||||
else if (counter_ == ini + inc * 7)
|
||||
{
|
||||
since_1998_texture_->setColor(color_[7].r, color_[7].g, color_[7].b);
|
||||
}
|
||||
|
||||
else if (counter_ == init_fade_ + inc * 0)
|
||||
{
|
||||
jailgames_texture_->setColor(color_[6].r, color_[6].g, color_[6].b);
|
||||
since_1998_texture_->setColor(color_[6].r, color_[6].g, color_[6].b);
|
||||
}
|
||||
|
||||
else if (counter_ == init_fade_ + inc * 1)
|
||||
{
|
||||
jailgames_texture_->setColor(color_[5].r, color_[5].g, color_[5].b);
|
||||
since_1998_texture_->setColor(color_[5].r, color_[5].g, color_[5].b);
|
||||
}
|
||||
|
||||
else if (counter_ == init_fade_ + inc * 2)
|
||||
{
|
||||
jailgames_texture_->setColor(color_[4].r, color_[4].g, color_[4].b);
|
||||
since_1998_texture_->setColor(color_[4].r, color_[4].g, color_[4].b);
|
||||
}
|
||||
|
||||
else if (counter_ == init_fade_ + inc * 3)
|
||||
{
|
||||
jailgames_texture_->setColor(color_[3].r, color_[3].g, color_[3].b);
|
||||
since_1998_texture_->setColor(color_[3].r, color_[3].g, color_[3].b);
|
||||
}
|
||||
|
||||
else if (counter_ == init_fade_ + inc * 4)
|
||||
{
|
||||
jailgames_texture_->setColor(color_[2].r, color_[2].g, color_[2].b);
|
||||
since_1998_texture_->setColor(color_[2].r, color_[2].g, color_[2].b);
|
||||
}
|
||||
|
||||
else if (counter_ == init_fade_ + inc * 5)
|
||||
{
|
||||
jailgames_texture_->setColor(color_[1].r, color_[1].g, color_[1].b);
|
||||
since_1998_texture_->setColor(color_[1].r, color_[1].g, color_[1].b);
|
||||
}
|
||||
|
||||
else if (counter_ == init_fade_ + inc * 6)
|
||||
{
|
||||
jailgames_texture_->setColor(color_[0].r, color_[0].g, color_[0].b);
|
||||
since_1998_texture_->setColor(color_[0].r, color_[0].g, color_[0].b);
|
||||
}
|
||||
}
|
||||
|
||||
// Actualiza las variables
|
||||
void Logo::update()
|
||||
{
|
||||
// Comprueba que la diferencia de ticks sea mayor a la velocidad del juego
|
||||
if (SDL_GetTicks() - ticks_ > ticks_speed_)
|
||||
{
|
||||
// Actualiza el contador de ticks
|
||||
ticks_ = SDL_GetTicks();
|
||||
|
||||
// Comprueba las entradas
|
||||
checkInput();
|
||||
|
||||
// Incrementa el contador
|
||||
counter_++;
|
||||
|
||||
// Gestiona el logo de JAILGAME
|
||||
updateJAILGAMES();
|
||||
|
||||
// Gestiona el color de las texturas
|
||||
updateTextureColors();
|
||||
|
||||
screen_->update();
|
||||
|
||||
// Comprueba si ha terminado el logo
|
||||
if (counter_ == end_logo_ + post_logo_)
|
||||
{
|
||||
endSection();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Dibuja en pantalla
|
||||
void Logo::render()
|
||||
{
|
||||
// Prepara para empezar a dibujar en la textura de juego
|
||||
screen_->start();
|
||||
|
||||
// Limpia la pantalla
|
||||
screen_->clean();
|
||||
|
||||
// Dibuja los objetos
|
||||
for (auto s : jailgames_sprite_)
|
||||
{
|
||||
s->render();
|
||||
}
|
||||
since_1998_sprite_->render();
|
||||
|
||||
// Vuelca el contenido del renderizador en pantalla
|
||||
screen_->render();
|
||||
}
|
||||
|
||||
// Bucle para el logo del juego
|
||||
void Logo::run()
|
||||
{
|
||||
// Detiene la música
|
||||
JA_StopMusic();
|
||||
|
||||
while (options.section.name == SECTION_LOGO)
|
||||
{
|
||||
update();
|
||||
checkEvents();
|
||||
render();
|
||||
}
|
||||
}
|
||||
|
||||
// Cambia la paleta
|
||||
void Logo::switchPalette()
|
||||
{
|
||||
options.palette = options.palette == p_zxspectrum ? p_zxarne : p_zxspectrum;
|
||||
}
|
||||
|
||||
// Termina la sección
|
||||
void Logo::endSection()
|
||||
{
|
||||
if (options.section.subsection == SUBSECTION_LOGO_TO_TITLE)
|
||||
{
|
||||
options.section.name = SECTION_TITLE;
|
||||
}
|
||||
|
||||
else if (options.section.subsection == SUBSECTION_LOGO_TO_INTRO)
|
||||
{
|
||||
options.section.name = SECTION_LOADING_SCREEN;
|
||||
}
|
||||
}
|
||||
73
source/logo.h
Normal file
73
source/logo.h
Normal file
@@ -0,0 +1,73 @@
|
||||
#pragma once
|
||||
|
||||
#include <SDL2/SDL_events.h> // for SDL_Event
|
||||
#include <SDL2/SDL_render.h> // for SDL_Renderer
|
||||
#include <SDL2/SDL_stdinc.h> // for Uint32
|
||||
#include <vector> // for vector
|
||||
class Asset; // lines 8-8
|
||||
class Input; // lines 9-9
|
||||
class Resource; // lines 10-10
|
||||
class Screen; // lines 11-11
|
||||
class Sprite; // lines 12-12
|
||||
class Texture; // lines 13-13
|
||||
struct color_t;
|
||||
struct options_t;
|
||||
struct section_t;
|
||||
|
||||
class Logo
|
||||
{
|
||||
private:
|
||||
// Objetos y punteros
|
||||
Screen *screen_; // Objeto encargado de dibujar en pantalla
|
||||
SDL_Renderer *renderer_; // El renderizador de la ventana
|
||||
Resource *resource_; // Objeto con los recursos
|
||||
Asset *asset_; // Objeto con los ficheros de recursos
|
||||
Input *input_; // Objeto pata gestionar la entrada
|
||||
Texture *jailgames_texture_; // Textura con los graficos "JAILGAMES"
|
||||
Texture *since_1998_texture_; // Textura con los graficos "Since 1998"
|
||||
std::vector<Sprite *> jailgames_sprite_; // Vector con los sprites de cada linea que forman el bitmap JAILGAMES
|
||||
Sprite *since_1998_sprite_; // Sprite para manejar la textura2
|
||||
|
||||
// Variables
|
||||
std::vector<color_t> color_; // Vector con los colores para el fade
|
||||
int counter_ = 0; // Contador
|
||||
Uint32 ticks_ = 0; // Contador de ticks para ajustar la velocidad del programa
|
||||
Uint32 ticks_speed_ = 15; // Velocidad a la que se repiten los bucles del programa
|
||||
int init_fade_ = 300; // Tiempo del contador cuando inicia el fade a negro
|
||||
int end_logo_ = 400; // Tiempo del contador para terminar el logo
|
||||
int post_logo_ = 20; // Tiempo que dura el logo con el fade al maximo
|
||||
|
||||
// Actualiza las variables
|
||||
void update();
|
||||
|
||||
// Dibuja en pantalla
|
||||
void render();
|
||||
|
||||
// Comprueba el manejador de eventos
|
||||
void checkEvents();
|
||||
|
||||
// Comprueba las entradas
|
||||
void checkInput();
|
||||
|
||||
// Gestiona el logo de JAILGAME
|
||||
void updateJAILGAMES();
|
||||
|
||||
// Gestiona el color de las texturas
|
||||
void updateTextureColors();
|
||||
|
||||
// Cambia la paleta
|
||||
void switchPalette();
|
||||
|
||||
// Termina la sección
|
||||
void endSection();
|
||||
|
||||
public:
|
||||
// Constructor
|
||||
Logo();
|
||||
|
||||
// Destructor
|
||||
~Logo();
|
||||
|
||||
// Bucle principal
|
||||
void run();
|
||||
};
|
||||
@@ -5,26 +5,14 @@ Empezado en Castalla el 01/07/2022.
|
||||
|
||||
*/
|
||||
|
||||
#include <memory>
|
||||
#include "director.h"
|
||||
#include <iostream>
|
||||
|
||||
using namespace std;
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
cout << "Starting the game..." << endl;
|
||||
|
||||
// Crea el objeto Director
|
||||
Director *director = new Director(argc, argv);
|
||||
auto director = std::make_unique<Director>(argc, const_cast<const char **>(argv));
|
||||
|
||||
// Bucle principal
|
||||
director->run();
|
||||
|
||||
// Destruye el objeto Director
|
||||
delete director;
|
||||
director = nullptr;
|
||||
|
||||
cout << "\nShutting down the game..." << endl;
|
||||
|
||||
return 0;
|
||||
return director->run();
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
#include "../const.h"
|
||||
#include "movingsprite.h"
|
||||
#include "texture.h" // Para Texture
|
||||
|
||||
// Constructor
|
||||
MovingSprite::MovingSprite(float x, float y, int w, int h, float velx, float vely, float accelx, float accely, Texture *texture, SDL_Renderer *renderer)
|
||||
@@ -1,10 +1,10 @@
|
||||
#pragma once
|
||||
|
||||
#include <SDL2/SDL.h>
|
||||
#include "sprite.h"
|
||||
|
||||
#ifndef MOVINGSPRITE_H
|
||||
#define MOVINGSPRITE_H
|
||||
#include <SDL2/SDL_rect.h> // Para SDL_Rect, SDL_Point
|
||||
#include <SDL2/SDL_render.h> // Para SDL_RendererFlip, SDL_Renderer
|
||||
#include <SDL2/SDL_stdinc.h> // Para Uint16
|
||||
#include "sprite.h" // Para Sprite
|
||||
class Texture;
|
||||
|
||||
// Clase MovingSprite. Añade posicion y velocidad en punto flotante
|
||||
class MovingSprite : public Sprite
|
||||
@@ -182,6 +182,4 @@ public:
|
||||
|
||||
// Devuelve el incremento en el eje X en pixels
|
||||
int getIncX();
|
||||
};
|
||||
|
||||
#endif
|
||||
};
|
||||
302
source/notifier.cpp
Normal file
302
source/notifier.cpp
Normal file
@@ -0,0 +1,302 @@
|
||||
#include "notifier.h"
|
||||
#include <SDL2/SDL_blendmode.h> // Para SDL_BLENDMODE_BLEND
|
||||
#include <string> // Para basic_string, string, char_traits
|
||||
#include "jail_audio.h" // Para JA_DeleteSound, JA_LoadSound, JA_Pla...
|
||||
#include "sprite.h" // Para Sprite
|
||||
#include "text.h" // Para Text
|
||||
#include "texture.h" // Para Texture
|
||||
#include "screen.h"
|
||||
#include "options.h"
|
||||
|
||||
// [SINGLETON]
|
||||
Notifier *Notifier::notifier_ = nullptr;
|
||||
|
||||
// [SINGLETON] Crearemos el objeto con esta función estática
|
||||
void Notifier::init(std::string iconFile, std::string bitmapFile, std::string textFile, std::string soundFile)
|
||||
{
|
||||
Notifier::notifier_ = new Notifier(iconFile, bitmapFile, textFile, soundFile);
|
||||
}
|
||||
|
||||
// [SINGLETON] Destruiremos el objeto con esta función estática
|
||||
void Notifier::destroy()
|
||||
{
|
||||
delete Notifier::notifier_;
|
||||
}
|
||||
|
||||
// [SINGLETON] Con este método obtenemos el objeto y podemos trabajar con él
|
||||
Notifier *Notifier::get()
|
||||
{
|
||||
return Notifier::notifier_;
|
||||
}
|
||||
|
||||
// Constructor
|
||||
Notifier::Notifier(std::string iconFile, std::string bitmapFile, std::string textFile, std::string soundFile)
|
||||
{
|
||||
// Inicializa variables
|
||||
renderer_ = Screen::get()->getRenderer();
|
||||
bg_color_ = options.notifications.color;
|
||||
wait_time_ = 300;
|
||||
|
||||
// Crea objetos
|
||||
icon_texture_ = new Texture(renderer_, iconFile);
|
||||
text_texture_ = new Texture(renderer_, bitmapFile);
|
||||
text_ = new Text(textFile, text_texture_, renderer_);
|
||||
sound_ = JA_LoadSound(soundFile.c_str());
|
||||
}
|
||||
|
||||
// Destructor
|
||||
Notifier::~Notifier()
|
||||
{
|
||||
// Libera la memoria de los objetos
|
||||
delete text_texture_;
|
||||
delete icon_texture_;
|
||||
delete text_;
|
||||
JA_DeleteSound(sound_);
|
||||
|
||||
for (auto notification : notifications_)
|
||||
{
|
||||
delete notification.sprite;
|
||||
delete notification.texture;
|
||||
}
|
||||
}
|
||||
|
||||
// Dibuja las notificaciones por pantalla
|
||||
void Notifier::render()
|
||||
{
|
||||
if (active())
|
||||
{
|
||||
for (auto it = notifications_.rbegin(); it != notifications_.rend(); ++it)
|
||||
{
|
||||
it->sprite->render();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Actualiza el estado de las notificaiones
|
||||
void Notifier::update()
|
||||
{
|
||||
for (int i = 0; i < (int)notifications_.size(); ++i)
|
||||
{
|
||||
// Si la notificación anterior está "saliendo", no hagas nada
|
||||
if (i > 0)
|
||||
{
|
||||
if (notifications_[i - 1].state == ns_rising)
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
notifications_[i].counter++;
|
||||
|
||||
// Hace sonar la notificación en el primer frame
|
||||
if (notifications_[i].counter == 1)
|
||||
{
|
||||
if (options.notifications.sound)
|
||||
{
|
||||
if (notifications_[i].state == ns_rising)
|
||||
{ // Reproduce el sonido de la notificación
|
||||
JA_PlaySound(sound_);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Comprueba los estados
|
||||
if (notifications_[i].state == ns_rising)
|
||||
{
|
||||
const float step = ((float)notifications_[i].counter / notifications_[i].travelDist);
|
||||
const int alpha = 255 * step;
|
||||
|
||||
if (options.notifications.posV == pos_top)
|
||||
{
|
||||
notifications_[i].rect.y++;
|
||||
}
|
||||
else
|
||||
{
|
||||
notifications_[i].rect.y--;
|
||||
}
|
||||
notifications_[i].texture->setAlpha(alpha);
|
||||
|
||||
if (notifications_[i].rect.y == notifications_[i].y)
|
||||
{
|
||||
notifications_[i].state = ns_stay;
|
||||
notifications_[i].texture->setAlpha(255);
|
||||
notifications_[i].counter = 0;
|
||||
}
|
||||
}
|
||||
|
||||
else if (notifications_[i].state == ns_stay)
|
||||
{
|
||||
if (notifications_[i].counter == wait_time_)
|
||||
{
|
||||
notifications_[i].state = ns_vanishing;
|
||||
notifications_[i].counter = 0;
|
||||
}
|
||||
}
|
||||
else if (notifications_[i].state == ns_vanishing)
|
||||
{
|
||||
|
||||
const float step = (notifications_[i].counter / (float)notifications_[i].travelDist);
|
||||
const int alpha = 255 * (1 - step);
|
||||
|
||||
if (options.notifications.posV == pos_top)
|
||||
{
|
||||
notifications_[i].rect.y--;
|
||||
}
|
||||
else
|
||||
{
|
||||
notifications_[i].rect.y++;
|
||||
}
|
||||
notifications_[i].texture->setAlpha(alpha);
|
||||
|
||||
if (notifications_[i].rect.y == notifications_[i].y - notifications_[i].travelDist)
|
||||
{
|
||||
notifications_[i].state = ns_finished;
|
||||
}
|
||||
}
|
||||
|
||||
notifications_[i].sprite->setRect(notifications_[i].rect);
|
||||
}
|
||||
|
||||
clearFinishedNotifications();
|
||||
}
|
||||
|
||||
// Elimina las notificaciones finalizadas
|
||||
void Notifier::clearFinishedNotifications()
|
||||
{
|
||||
for (int i = (int)notifications_.size() - 1; i >= 0; --i)
|
||||
{
|
||||
if (notifications_[i].state == ns_finished)
|
||||
{
|
||||
delete notifications_[i].sprite;
|
||||
delete notifications_[i].texture;
|
||||
notifications_.erase(notifications_.begin() + i);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Muestra una notificación de texto por pantalla;
|
||||
void Notifier::show(std::string text1, std::string text2, int icon)
|
||||
{
|
||||
// Inicializa variables
|
||||
const int iconSize = 16;
|
||||
const int padding = text_->getCharacterSize();
|
||||
const int iconSpace = icon >= 0 ? iconSize + padding : 0;
|
||||
const std::string txt = text1.length() > text2.length() ? text1 : text2;
|
||||
const int width = text_->lenght(txt) + (padding * 2) + iconSpace;
|
||||
const int height = (text_->getCharacterSize() * 2) + (padding * 2);
|
||||
|
||||
// Posición horizontal
|
||||
int despH = 0;
|
||||
if (options.notifications.posH == pos_left)
|
||||
{
|
||||
despH = padding;
|
||||
}
|
||||
else if (options.notifications.posH == pos_middle)
|
||||
{
|
||||
despH = ((options.screen.windowWidth * options.windowSize) / 2 - (width / 2));
|
||||
}
|
||||
else
|
||||
{
|
||||
despH = (options.screen.windowWidth * options.windowSize) - width - padding;
|
||||
}
|
||||
|
||||
// Posición vertical
|
||||
int despV = 0;
|
||||
if (options.notifications.posV == pos_top)
|
||||
{
|
||||
despV = padding;
|
||||
}
|
||||
else
|
||||
{
|
||||
despV = (options.screen.windowHeight * options.windowSize) - height - padding;
|
||||
}
|
||||
|
||||
const int travelDist = height + padding;
|
||||
|
||||
// Offset
|
||||
int offset = 0;
|
||||
if (options.notifications.posV == pos_top)
|
||||
{
|
||||
offset = (int)notifications_.size() > 0 ? notifications_.back().y + travelDist : despV;
|
||||
}
|
||||
else
|
||||
{
|
||||
offset = (int)notifications_.size() > 0 ? notifications_.back().y - travelDist : despV;
|
||||
}
|
||||
|
||||
// Crea la notificacion
|
||||
notification_t n;
|
||||
|
||||
// Inicializa variables
|
||||
n.y = offset;
|
||||
n.travelDist = travelDist;
|
||||
n.counter = 0;
|
||||
n.state = ns_rising;
|
||||
n.text1 = text1;
|
||||
n.text2 = text2;
|
||||
if (options.notifications.posV == pos_top)
|
||||
{
|
||||
n.rect = {despH, offset - travelDist, width, height};
|
||||
}
|
||||
else
|
||||
{
|
||||
n.rect = {despH, offset + travelDist, width, height};
|
||||
}
|
||||
|
||||
// Crea la textura
|
||||
n.texture = new Texture(renderer_);
|
||||
n.texture->createBlank(renderer_, width, height, SDL_TEXTUREACCESS_TARGET);
|
||||
n.texture->setBlendMode(SDL_BLENDMODE_BLEND);
|
||||
|
||||
// Prepara para dibujar en la textura
|
||||
n.texture->setAsRenderTarget(renderer_);
|
||||
|
||||
// Dibuja el fondo de la notificación
|
||||
SDL_SetRenderDrawColor(renderer_, bg_color_.r, bg_color_.g, bg_color_.b, 255);
|
||||
SDL_Rect rect;
|
||||
rect = {4, 0, width - (4 * 2), height};
|
||||
SDL_RenderFillRect(renderer_, &rect);
|
||||
|
||||
rect = {4 / 2, 1, width - 4, height - 2};
|
||||
SDL_RenderFillRect(renderer_, &rect);
|
||||
|
||||
rect = {1, 4 / 2, width - 2, height - 4};
|
||||
SDL_RenderFillRect(renderer_, &rect);
|
||||
|
||||
rect = {0, 4, width, height - (4 * 2)};
|
||||
SDL_RenderFillRect(renderer_, &rect);
|
||||
|
||||
// Dibuja el icono de la notificación
|
||||
if (icon >= 0)
|
||||
{
|
||||
Sprite *sp = new Sprite({0, 0, iconSize, iconSize}, icon_texture_, renderer_);
|
||||
sp->setPos({padding, padding, iconSize, iconSize});
|
||||
sp->setSpriteClip({iconSize * (icon % 10), iconSize * (icon / 10), iconSize, iconSize});
|
||||
sp->render();
|
||||
delete sp;
|
||||
}
|
||||
|
||||
// Escribe el texto de la notificación
|
||||
color_t color = {255, 255, 255};
|
||||
if (text2 != "")
|
||||
{ // Dos lineas de texto
|
||||
text_->writeColored(padding + iconSpace, padding, text1, color);
|
||||
text_->writeColored(padding + iconSpace, padding + text_->getCharacterSize() + 1, text2, color);
|
||||
}
|
||||
else
|
||||
{ // Una linea de texto
|
||||
text_->writeColored(padding + iconSpace, (height / 2) - (text_->getCharacterSize() / 2), text1, color);
|
||||
}
|
||||
|
||||
// Deja de dibujar en la textura
|
||||
SDL_SetRenderTarget(renderer_, nullptr);
|
||||
|
||||
// Crea el sprite de la notificación
|
||||
n.sprite = new Sprite(n.rect, n.texture, renderer_);
|
||||
|
||||
// Deja la notificación invisible
|
||||
n.texture->setAlpha(0);
|
||||
|
||||
// Añade la notificación a la lista
|
||||
notifications_.push_back(n);
|
||||
}
|
||||
95
source/notifier.h
Normal file
95
source/notifier.h
Normal file
@@ -0,0 +1,95 @@
|
||||
#pragma once
|
||||
|
||||
#include <SDL2/SDL_rect.h> // Para SDL_Rect
|
||||
#include <SDL2/SDL_render.h> // Para SDL_Renderer
|
||||
#include <string> // Para basic_string, string
|
||||
#include <vector> // Para vector
|
||||
#include "utils.h" // Para color_t
|
||||
class Sprite;
|
||||
class Text;
|
||||
class Texture;
|
||||
struct JA_Sound_t;
|
||||
|
||||
class Notifier
|
||||
{
|
||||
private:
|
||||
// [SINGLETON] Objeto notifier
|
||||
static Notifier *notifier_;
|
||||
|
||||
enum notification_state_e
|
||||
{
|
||||
ns_rising,
|
||||
ns_stay,
|
||||
ns_vanishing,
|
||||
ns_finished
|
||||
};
|
||||
|
||||
enum notification_position_e
|
||||
{
|
||||
upperLeft,
|
||||
upperCenter,
|
||||
upperRight,
|
||||
middleLeft,
|
||||
middleRight,
|
||||
bottomLeft,
|
||||
bottomCenter,
|
||||
bottomRight
|
||||
};
|
||||
|
||||
struct notification_t
|
||||
{
|
||||
std::string text1;
|
||||
std::string text2;
|
||||
int counter;
|
||||
notification_state_e state;
|
||||
notification_position_e position;
|
||||
Texture *texture;
|
||||
Sprite *sprite;
|
||||
SDL_Rect rect;
|
||||
int y;
|
||||
int travelDist;
|
||||
};
|
||||
|
||||
// Objetos y punteros
|
||||
SDL_Renderer *renderer_; // El renderizador de la ventana
|
||||
Texture *text_texture_; // Textura para la fuente de las notificaciones
|
||||
Texture *icon_texture_; // Textura para los iconos de las notificaciones
|
||||
Text *text_; // Objeto para dibujar texto
|
||||
|
||||
// Variables
|
||||
color_t bg_color_; // Color de fondo de las notificaciones
|
||||
int wait_time_; // Tiempo que se ve la notificación
|
||||
std::vector<notification_t> notifications_; // La lista de notificaciones activas
|
||||
JA_Sound_t *sound_; // Sonido a reproducir cuando suena la notificación
|
||||
|
||||
// Elimina las notificaciones finalizadas
|
||||
void clearFinishedNotifications();
|
||||
|
||||
// Constructor
|
||||
Notifier(std::string iconFile, std::string bitmapFile, std::string textFile, std::string soundFile);
|
||||
|
||||
// Destructor
|
||||
~Notifier();
|
||||
|
||||
public:
|
||||
// [SINGLETON] Crearemos el objeto con esta función estática
|
||||
static void init(std::string iconFile, std::string bitmapFile, std::string textFile, std::string soundFile);
|
||||
|
||||
// [SINGLETON] Destruiremos el objeto con esta función estática
|
||||
static void destroy();
|
||||
|
||||
// [SINGLETON] Con este método obtenemos el objeto y podemos trabajar con él
|
||||
static Notifier *get();
|
||||
|
||||
// Dibuja las notificaciones por pantalla
|
||||
void render();
|
||||
|
||||
// Actualiza el estado de las notificaiones
|
||||
void update();
|
||||
|
||||
// Muestra una notificación de texto por pantalla;
|
||||
void show(std::string text1 = "", std::string text2 = "", int icon = -1);
|
||||
|
||||
// Getters
|
||||
bool active() const { return !notifications_.empty(); }
|
||||
};
|
||||
@@ -1,169 +0,0 @@
|
||||
#include "jail_engine/jscore.h"
|
||||
#include "online.h"
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
#include <sstream>
|
||||
|
||||
// Constructor
|
||||
Online::Online(options_t *options)
|
||||
{
|
||||
this->options = options;
|
||||
|
||||
if (options->console)
|
||||
{
|
||||
std::cout << "ONLINE object created\n"
|
||||
<< std::endl;
|
||||
}
|
||||
|
||||
allData = "";
|
||||
statsData = "";
|
||||
cheevosData = "";
|
||||
|
||||
STATS_FLAG_INI = ";STATS_FLAG_INI;";
|
||||
STATS_FLAG_END = ";STATS_FLAG_END;";
|
||||
CHEEVOS_FLAG_INI = ";CHEEVOS_FLAG_INI;";
|
||||
CHEEVOS_FLAG_END = ";CHEEVOS_FLAG_END;";
|
||||
|
||||
dataCached = false;
|
||||
dataSaved = "";
|
||||
getData();
|
||||
}
|
||||
|
||||
// Destructor
|
||||
Online::~Online()
|
||||
{
|
||||
sendData();
|
||||
}
|
||||
|
||||
// Obtiene todos los datos y los coloca en sus respectivas variables
|
||||
void Online::getData()
|
||||
{
|
||||
// Si el usuario es distinto del que hay cacheado, marca la cache como invalida y borra los datos
|
||||
if (jailerID.compare(options->online.jailerID) != 0)
|
||||
{
|
||||
dataCached = false;
|
||||
clearData();
|
||||
}
|
||||
|
||||
// Si los datos ya estan cacheados, no hace nada
|
||||
if (dataCached)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
// Si las opciones online estan activadas, obtiene los datos desde el servidor
|
||||
if (options->online.enabled)
|
||||
{
|
||||
allData = jscore::getUserData(options->online.gameID, options->online.jailerID);
|
||||
jailerID = options->online.jailerID;
|
||||
}
|
||||
|
||||
// Si no ha podido obtener los datos del servidor, no hace nada
|
||||
if (allData.length() == 0)
|
||||
{
|
||||
if (options->console)
|
||||
{
|
||||
std::cout << "NO DATA\n"
|
||||
<< std::endl;
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
// Obtiene el inicio y el fin de la cadena con las estadisticas
|
||||
const size_t statsIni = allData.find(STATS_FLAG_INI) + STATS_FLAG_INI.length();
|
||||
const size_t statsEnd = allData.find(STATS_FLAG_END);
|
||||
const size_t statsDataLenght = statsEnd - statsIni;
|
||||
|
||||
// Obtiene la cadena con las estadisticas
|
||||
if (statsIni != std::string::npos && statsEnd != std::string::npos)
|
||||
statsData = allData.substr(statsIni, statsDataLenght);
|
||||
|
||||
// Obtiene el inicio y el fin de la cadena con los logros
|
||||
const size_t cheevosIni = allData.find(CHEEVOS_FLAG_INI) + CHEEVOS_FLAG_INI.length();
|
||||
const size_t cheevosEnd = allData.find(CHEEVOS_FLAG_END);
|
||||
const size_t cheevosDataLenght = cheevosEnd - cheevosIni;
|
||||
|
||||
// Obtiene la cadena con los logros
|
||||
if (cheevosIni != std::string::npos && cheevosEnd != std::string::npos)
|
||||
cheevosData = allData.substr(cheevosIni, cheevosDataLenght);
|
||||
|
||||
dataCached = true;
|
||||
printData("LOADING");
|
||||
}
|
||||
|
||||
// Coloca todos los datos desde las variables en la cadena allData
|
||||
void Online::sendData()
|
||||
{
|
||||
allData = STATS_FLAG_INI + statsData + STATS_FLAG_END + CHEEVOS_FLAG_INI + cheevosData + CHEEVOS_FLAG_END;
|
||||
const bool newData = allData.compare(dataSaved) == 0 ? false : true;
|
||||
if (options->online.enabled && newData)
|
||||
{
|
||||
jscore::setUserData(options->online.gameID, options->online.jailerID, allData);
|
||||
dataSaved = allData;
|
||||
printData("SAVING");
|
||||
}
|
||||
}
|
||||
|
||||
// Obtiene las estadísticas guardadas en el servidor
|
||||
std::string Online::getStats()
|
||||
{
|
||||
getData();
|
||||
return statsData;
|
||||
}
|
||||
|
||||
// Guarda las estadísticas en el servidor
|
||||
void Online::setStats(std::string data)
|
||||
{
|
||||
// getData();
|
||||
statsData = data;
|
||||
// setAllData();
|
||||
}
|
||||
|
||||
// Obtiene los logros guardadas en el servidor
|
||||
std::string Online::getCheevos()
|
||||
{
|
||||
getData();
|
||||
return cheevosData;
|
||||
}
|
||||
|
||||
// Guarda los logros en el servidor
|
||||
void Online::setCheevos(std::string data)
|
||||
{
|
||||
getData();
|
||||
cheevosData = data;
|
||||
sendData();
|
||||
}
|
||||
|
||||
// Imprime información de diagnóstico
|
||||
void Online::printData(std::string text)
|
||||
{
|
||||
static int counter = 0;
|
||||
if (options->console)
|
||||
{
|
||||
std::cout << "mode is: " << text << " (" << counter << ")" << std::endl;
|
||||
std::cout << "allData: " << allData << std::endl;
|
||||
std::cout << "statsData: " << statsData << std::endl;
|
||||
std::cout << "cheevosData: " << cheevosData << std::endl;
|
||||
std::cout << "options->online.jailerID: " << options->online.jailerID << std::endl;
|
||||
std::cout << "options->online.enabled: " << options->online.enabled << std::endl;
|
||||
std::cout << std::endl;
|
||||
counter++;
|
||||
}
|
||||
}
|
||||
|
||||
// Elimina los datos del servidor
|
||||
void Online::eraseServerData()
|
||||
{
|
||||
if (options->online.enabled)
|
||||
{
|
||||
jscore::setUserData(options->online.gameID, options->online.jailerID, "");
|
||||
}
|
||||
}
|
||||
|
||||
// Limpia los datos almacenados en la caché
|
||||
void Online::clearData()
|
||||
{
|
||||
allData = "";
|
||||
statsData = "";
|
||||
cheevosData = "";
|
||||
}
|
||||
@@ -1,66 +0,0 @@
|
||||
#pragma once
|
||||
#include <SDL2/SDL.h>
|
||||
#include "jail_engine/utils.h"
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#ifndef ONLINE_H
|
||||
#define ONLINE_H
|
||||
|
||||
class Online
|
||||
{
|
||||
private:
|
||||
// Punteros y objetos
|
||||
options_t *options;
|
||||
|
||||
// Variables
|
||||
std::string allData; // La cadena entera de datos
|
||||
std::string statsData; // La cadena con los datos de las estadísticas
|
||||
std::string cheevosData; // La cadena con los datos de los logros
|
||||
|
||||
std::string STATS_FLAG_INI; // Marca para establecer el inicio de las estadísticas
|
||||
std::string STATS_FLAG_END; // Marca para establecer el final de las estadísticas
|
||||
std::string CHEEVOS_FLAG_INI; // Marca para establecer el inicio de los logros
|
||||
std::string CHEEVOS_FLAG_END; // Marca para establecer el final de los logros
|
||||
|
||||
bool dataCached; // Indica si se han obtenido los datos del servidor
|
||||
std::string dataSaved; // Contiene los datos que se han salvado en el servidor
|
||||
std::string jailerID; // ID del usuario cuyos datos estan cacheados
|
||||
|
||||
|
||||
// Imprime información de diagnóstico
|
||||
void printData(std::string text);
|
||||
|
||||
// Elimina los datos del servidor
|
||||
void eraseServerData();
|
||||
|
||||
// Limpia los datos almacenados en la caché
|
||||
void clearData();
|
||||
|
||||
public:
|
||||
// Constructor
|
||||
Online(options_t *options);
|
||||
|
||||
// Destructor
|
||||
~Online();
|
||||
|
||||
// Obtiene todos los datos y los coloca en sus respectivas variables
|
||||
void getData();
|
||||
|
||||
// Coloca todos los datos desde las variables en la cadena allData
|
||||
void sendData();
|
||||
|
||||
// Obtiene las estadísticas guardadas en el servidor
|
||||
std::string getStats();
|
||||
|
||||
// Guarda las estadísticas en el servidor
|
||||
void setStats(std::string data);
|
||||
|
||||
// Obtiene los logros guardadas en el servidor
|
||||
std::string getCheevos();
|
||||
|
||||
// Guarda los logros en el servidor
|
||||
void setCheevos(std::string data);
|
||||
};
|
||||
|
||||
#endif
|
||||
411
source/options.cpp
Normal file
411
source/options.cpp
Normal file
@@ -0,0 +1,411 @@
|
||||
#include "options.h"
|
||||
#include "const.h"
|
||||
#include "screen.h"
|
||||
#include <fstream> // Para basic_ofstream, basic_ifstream
|
||||
#include <iostream> // Para basic_ostream, operator<<, cout
|
||||
|
||||
// Variables
|
||||
options_t options;
|
||||
|
||||
bool setOptions(std::string var, std::string value);
|
||||
|
||||
void initOptions()
|
||||
{
|
||||
// Version del archivo de configuración
|
||||
options.configVersion = "v1.06.1";
|
||||
|
||||
// Opciones de control
|
||||
options.keys = ctrl_cursor;
|
||||
|
||||
// Opciones de video
|
||||
options.gameWidth = GAMECANVAS_WIDTH;
|
||||
options.gameHeight = GAMECANVAS_HEIGHT;
|
||||
options.videoMode = 0;
|
||||
options.windowSize = 3;
|
||||
options.filter = FILTER_NEAREST;
|
||||
options.shaders = false;
|
||||
options.vSync = true;
|
||||
options.integerScale = true;
|
||||
options.keepAspect = true;
|
||||
options.borderEnabled = true;
|
||||
options.borderWidth = 32;
|
||||
options.borderHeight = 24;
|
||||
options.palette = p_zxspectrum;
|
||||
|
||||
#ifdef GAME_CONSOLE
|
||||
options.windowSize = 2;
|
||||
#endif
|
||||
|
||||
// Estos valores no se guardan en el fichero de configuración
|
||||
options.console = false;
|
||||
#ifdef DEBUG
|
||||
options.console = true;
|
||||
#endif
|
||||
options.cheat.infiniteLives = false;
|
||||
options.cheat.invincible = false;
|
||||
options.cheat.jailEnabled = false;
|
||||
options.cheat.altSkin = false;
|
||||
options.stats.rooms = 0;
|
||||
options.stats.items = 0;
|
||||
|
||||
// Opciones de las notificaciones
|
||||
options.notifications.posV = pos_top;
|
||||
options.notifications.posH = pos_left;
|
||||
options.notifications.sound = true;
|
||||
options.notifications.color = {48, 48, 48};
|
||||
|
||||
#ifdef DEBUG
|
||||
options.section.name = SECTION_TITLE;
|
||||
options.section.subsection = SUBSECTION_LOGO_TO_INTRO;
|
||||
#else
|
||||
options.section.name = SECTION_LOGO;
|
||||
options.section.subsection = SUBSECTION_LOGO_TO_INTRO;
|
||||
#endif
|
||||
}
|
||||
|
||||
bool loadOptionsFromFile(const std::string &file_path)
|
||||
{
|
||||
// Indicador de éxito en la carga
|
||||
bool success = true;
|
||||
|
||||
// Versión actual del fichero
|
||||
const std::string configVersion = options.configVersion;
|
||||
options.configVersion = "";
|
||||
|
||||
// Variables para manejar el fichero
|
||||
std::string line;
|
||||
std::ifstream file(file_path);
|
||||
|
||||
// Si el fichero se puede abrir
|
||||
if (file.good())
|
||||
{
|
||||
// Procesa el fichero linea a linea
|
||||
if (options.console)
|
||||
{
|
||||
std::cout << "Reading file config.txt\n";
|
||||
}
|
||||
while (std::getline(file, line))
|
||||
{
|
||||
// Comprueba que la linea no sea un comentario
|
||||
if (line.substr(0, 1) != "#")
|
||||
{
|
||||
// Encuentra la posición del caracter '='
|
||||
int pos = line.find("=");
|
||||
// Procesa las dos subcadenas
|
||||
if (!setOptions(line.substr(0, pos), line.substr(pos + 1, line.length())))
|
||||
{
|
||||
if (options.console)
|
||||
{
|
||||
std::cout << "Warning: file config.txt\n";
|
||||
std::cout << "unknown parameter " << line.substr(0, pos).c_str() << std::endl;
|
||||
}
|
||||
success = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Cierra el fichero
|
||||
if (options.console)
|
||||
{
|
||||
std::cout << "Closing file config.txt\n\n";
|
||||
}
|
||||
file.close();
|
||||
}
|
||||
|
||||
// El fichero no existe
|
||||
else
|
||||
{ // Crea el fichero con los valores por defecto
|
||||
saveOptionsToFile(file_path);
|
||||
}
|
||||
|
||||
// Si la versión de fichero no coincide, crea un fichero nuevo con los valores por defecto
|
||||
if (configVersion != options.configVersion)
|
||||
{
|
||||
initOptions();
|
||||
saveOptionsToFile(file_path);
|
||||
}
|
||||
|
||||
// Normaliza los valores
|
||||
const bool a = options.videoMode == 0;
|
||||
const bool b = options.videoMode == SDL_WINDOW_FULLSCREEN;
|
||||
const bool c = options.videoMode == SDL_WINDOW_FULLSCREEN_DESKTOP;
|
||||
if (!(a || b || c))
|
||||
{
|
||||
options.videoMode = 0;
|
||||
}
|
||||
|
||||
if (options.windowSize < 1 || options.windowSize > 4)
|
||||
{
|
||||
options.windowSize = 3;
|
||||
}
|
||||
|
||||
return success;
|
||||
}
|
||||
|
||||
bool saveOptionsToFile(const std::string &file_path)
|
||||
{
|
||||
bool success = true;
|
||||
|
||||
// Crea y abre el fichero de texto
|
||||
std::ofstream file(file_path);
|
||||
|
||||
if (file.good())
|
||||
{
|
||||
if (options.console)
|
||||
{
|
||||
std::cout << file_path << " open for writing" << std::endl;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (options.console)
|
||||
{
|
||||
std::cout << file_path << " can't be opened" << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
// Escribe en el fichero
|
||||
file << "## VERSION\n";
|
||||
file << "configVersion=" + options.configVersion + "\n";
|
||||
|
||||
file << "\n## CONTROL OPTIONS\n";
|
||||
file << "## keys = CURSOR | OPQA | WASD\n";
|
||||
if (options.keys == ctrl_cursor)
|
||||
{
|
||||
file << "keys=CURSOR\n";
|
||||
}
|
||||
else if (options.keys == ctrl_opqa)
|
||||
{
|
||||
file << "keys=OPQA\n";
|
||||
}
|
||||
else if (options.keys == ctrl_wasd)
|
||||
{
|
||||
file << "keys=WASD\n";
|
||||
}
|
||||
|
||||
file << "\n## VISUAL OPTIONS\n";
|
||||
if (options.videoMode == 0)
|
||||
{
|
||||
file << "videoMode=0\n";
|
||||
}
|
||||
|
||||
else if (options.videoMode == SDL_WINDOW_FULLSCREEN)
|
||||
{
|
||||
file << "videoMode=SDL_WINDOW_FULLSCREEN\n";
|
||||
}
|
||||
|
||||
else if (options.videoMode == SDL_WINDOW_FULLSCREEN_DESKTOP)
|
||||
{
|
||||
file << "videoMode=SDL_WINDOW_FULLSCREEN_DESKTOP\n";
|
||||
}
|
||||
|
||||
file << "windowSize=" + std::to_string(options.windowSize) + "\n";
|
||||
|
||||
if (options.filter == FILTER_NEAREST)
|
||||
{
|
||||
file << "filter=FILTER_NEAREST\n";
|
||||
}
|
||||
else
|
||||
{
|
||||
file << "filter=FILTER_LINEAR\n";
|
||||
}
|
||||
|
||||
file << "shaders=" + boolToString(options.shaders) + "\n";
|
||||
file << "vSync=" + boolToString(options.vSync) + "\n";
|
||||
file << "integerScale=" + boolToString(options.integerScale) + "\n";
|
||||
file << "keepAspect=" + boolToString(options.keepAspect) + "\n";
|
||||
file << "borderEnabled=" + boolToString(options.borderEnabled) + "\n";
|
||||
file << "borderWidth=" + std::to_string(options.borderWidth) + "\n";
|
||||
file << "borderHeight=" + std::to_string(options.borderHeight) + "\n";
|
||||
file << "palette=" + std::to_string(options.palette) + "\n";
|
||||
|
||||
file << "\n## NOTIFICATION OPTIONS\n";
|
||||
file << "## notifications.posV = pos_top | pos_bottom\n";
|
||||
if (options.notifications.posV == pos_top)
|
||||
{
|
||||
file << "notifications.posV=pos_top\n";
|
||||
}
|
||||
else
|
||||
{
|
||||
file << "notifications.posV=pos_bottom\n";
|
||||
}
|
||||
|
||||
file << "## notifications.posH = pos_left | pos_middle | pos_right\n";
|
||||
if (options.notifications.posH == pos_left)
|
||||
{
|
||||
file << "notifications.posH=pos_left\n";
|
||||
}
|
||||
else if (options.notifications.posH == pos_middle)
|
||||
{
|
||||
file << "notifications.posH=pos_middle\n";
|
||||
}
|
||||
else
|
||||
{
|
||||
file << "notifications.posH=pos_right\n";
|
||||
}
|
||||
|
||||
file << "notifications.sound=" + boolToString(options.notifications.sound) + "\n";
|
||||
|
||||
// Cierra el fichero
|
||||
file.close();
|
||||
|
||||
return success;
|
||||
}
|
||||
|
||||
bool setOptions(std::string var, std::string value)
|
||||
{
|
||||
// Indicador de éxito en la asignación
|
||||
bool success = true;
|
||||
|
||||
if (var == "configVersion")
|
||||
{
|
||||
options.configVersion = value;
|
||||
}
|
||||
|
||||
else if (var == "keys")
|
||||
{
|
||||
if (value == "OPQA")
|
||||
{
|
||||
options.keys = ctrl_opqa;
|
||||
}
|
||||
else if (value == "WASD")
|
||||
{
|
||||
options.keys = ctrl_wasd;
|
||||
}
|
||||
else
|
||||
{
|
||||
options.keys = ctrl_cursor;
|
||||
}
|
||||
}
|
||||
|
||||
else if (var == "videoMode")
|
||||
{
|
||||
if (value == "SDL_WINDOW_FULLSCREEN_DESKTOP")
|
||||
{
|
||||
options.videoMode = SDL_WINDOW_FULLSCREEN_DESKTOP;
|
||||
}
|
||||
else if (value == "SDL_WINDOW_FULLSCREEN")
|
||||
{
|
||||
options.videoMode = SDL_WINDOW_FULLSCREEN;
|
||||
}
|
||||
else
|
||||
{
|
||||
options.videoMode = 0;
|
||||
}
|
||||
}
|
||||
|
||||
else if (var == "windowSize")
|
||||
{
|
||||
options.windowSize = std::stoi(value);
|
||||
if ((options.windowSize < 1) || (options.windowSize > 4))
|
||||
{
|
||||
options.windowSize = 3;
|
||||
}
|
||||
}
|
||||
|
||||
else if (var == "filter")
|
||||
{
|
||||
if (value == "FILTER_LINEAR")
|
||||
{
|
||||
options.filter = FILTER_LINEAR;
|
||||
}
|
||||
else
|
||||
{
|
||||
options.filter = FILTER_NEAREST;
|
||||
}
|
||||
}
|
||||
|
||||
else if (var == "shaders")
|
||||
{
|
||||
options.shaders = stringToBool(value);
|
||||
}
|
||||
|
||||
else if (var == "vSync")
|
||||
{
|
||||
options.vSync = stringToBool(value);
|
||||
}
|
||||
|
||||
else if (var == "integerScale")
|
||||
{
|
||||
options.integerScale = stringToBool(value);
|
||||
}
|
||||
|
||||
else if (var == "keepAspect")
|
||||
{
|
||||
options.keepAspect = stringToBool(value);
|
||||
}
|
||||
|
||||
else if (var == "borderEnabled")
|
||||
{
|
||||
options.borderEnabled = stringToBool(value);
|
||||
}
|
||||
|
||||
else if (var == "borderWidth")
|
||||
{
|
||||
options.borderWidth = std::stoi(value);
|
||||
}
|
||||
|
||||
else if (var == "borderHeight")
|
||||
{
|
||||
options.borderHeight = std::stoi(value);
|
||||
}
|
||||
|
||||
else if (var == "palette")
|
||||
{
|
||||
const int pal = std::stoi(value);
|
||||
|
||||
if (pal == 0)
|
||||
{
|
||||
options.palette = p_zxspectrum;
|
||||
}
|
||||
|
||||
else if (pal == 1)
|
||||
{
|
||||
options.palette = p_zxarne;
|
||||
}
|
||||
}
|
||||
|
||||
else if (var == "notifications.posH")
|
||||
{
|
||||
if (value == "pos_left")
|
||||
{
|
||||
options.notifications.posH = pos_left;
|
||||
}
|
||||
else if (value == "pos_middle")
|
||||
{
|
||||
options.notifications.posH = pos_middle;
|
||||
}
|
||||
else
|
||||
{
|
||||
options.notifications.posH = pos_right;
|
||||
}
|
||||
}
|
||||
|
||||
else if (var == "notifications.posV")
|
||||
{
|
||||
if (value == "pos_top")
|
||||
{
|
||||
options.notifications.posV = pos_top;
|
||||
}
|
||||
else
|
||||
{
|
||||
options.notifications.posV = pos_bottom;
|
||||
}
|
||||
}
|
||||
|
||||
else if (var == "notifications.sound")
|
||||
{
|
||||
options.notifications.sound = stringToBool(value);
|
||||
}
|
||||
|
||||
else if (var == "" || var.substr(0, 1) == "#")
|
||||
{
|
||||
}
|
||||
|
||||
else
|
||||
{
|
||||
success = false;
|
||||
}
|
||||
|
||||
return success;
|
||||
}
|
||||
101
source/options.h
Normal file
101
source/options.h
Normal file
@@ -0,0 +1,101 @@
|
||||
#pragma once
|
||||
|
||||
#include <SDL2/SDL_rect.h> // Para SDL_Rect, SDL_Point
|
||||
#include <SDL2/SDL_stdinc.h> // Para Uint8, Uint32
|
||||
#include <string> // Para string, basic_string
|
||||
#include "utils.h"
|
||||
|
||||
// Posiciones de las notificaciones
|
||||
enum not_pos_e
|
||||
{
|
||||
pos_top,
|
||||
pos_bottom,
|
||||
pos_left,
|
||||
pos_middle,
|
||||
pos_right
|
||||
};
|
||||
|
||||
// Tipos de control de teclado
|
||||
enum ctrl_schem_e
|
||||
{
|
||||
ctrl_cursor,
|
||||
ctrl_opqa,
|
||||
ctrl_wasd
|
||||
};
|
||||
|
||||
// Estructura para las opciones de las notificaciones
|
||||
struct op_notification_t
|
||||
{
|
||||
not_pos_e posH; // Ubicación de las notificaciones en pantalla
|
||||
not_pos_e posV; // Ubicación de las notificaciones en pantalla
|
||||
bool sound; // Indica si las notificaciones suenan
|
||||
color_t color; // Color de las notificaciones
|
||||
};
|
||||
|
||||
// Estructura para saber la seccion y subseccion del programa
|
||||
struct section_t
|
||||
{
|
||||
Uint8 name;
|
||||
Uint8 subsection;
|
||||
};
|
||||
|
||||
// Estructura para albergar trucos
|
||||
struct cheat_t
|
||||
{
|
||||
bool infiniteLives; // Indica si el jugador dispone de vidas infinitas
|
||||
bool invincible; // Indica si el jugador puede morir
|
||||
bool jailEnabled; // Indica si la Jail está abierta
|
||||
bool altSkin; // Indicxa si se usa una skin diferente para el jugador
|
||||
};
|
||||
|
||||
// Estructura para almacenar estadísticas
|
||||
struct op_stats_t
|
||||
{
|
||||
int rooms; // Cantidad de habitaciones visitadas
|
||||
int items; // Cantidad de items obtenidos
|
||||
std::string worstNightmare; // Habitación con más muertes acumuladas
|
||||
};
|
||||
|
||||
// Estructura con opciones de la pantalla
|
||||
struct op_screen_t
|
||||
{
|
||||
int windowWidth; // Ancho de la ventana
|
||||
int windowHeight; // Alto de la ventana
|
||||
};
|
||||
|
||||
// Estructura con todas las opciones de configuración del programa
|
||||
struct options_t
|
||||
{
|
||||
std::string configVersion; // Versión del programa. Sirve para saber si las opciones son compatibles
|
||||
Uint32 videoMode; // Contiene el valor del modo de pantalla completa
|
||||
int windowSize; // Contiene el valor por el que se multiplica el tamaño de la ventana
|
||||
Uint32 filter; // Filtro usado para el escalado de la imagen
|
||||
bool vSync; // Indica si se quiere usar vsync o no
|
||||
bool shaders; // Indica si se van a usar shaders o no
|
||||
int gameWidth; // Ancho de la resolucion nativa del juego
|
||||
int gameHeight; // Alto de la resolucion nativa del juego
|
||||
bool integerScale; // Indica si el escalado de la imagen ha de ser entero en el modo a pantalla completa
|
||||
bool keepAspect; // Indica si se ha de mantener la relación de aspecto al poner el modo a pantalla completa
|
||||
bool borderEnabled; // Indica si ha de mostrar el borde en el modo de ventana
|
||||
int borderWidth; // Cantidad de pixels que se añade en el borde de la ventana
|
||||
int borderHeight; // Cantidad de pixels que se añade en el borde de la ventana
|
||||
palette_e palette; // Paleta de colores a usar en el juego
|
||||
bool console; // Indica si ha de mostrar información por la consola de texto
|
||||
cheat_t cheat; // Contiene trucos y ventajas para el juego
|
||||
op_stats_t stats; // Datos con las estadisticas de juego
|
||||
op_notification_t notifications; // Opciones relativas a las notificaciones;
|
||||
op_screen_t screen; // Opciones relativas a la clase screen
|
||||
ctrl_schem_e keys; // Teclas usadas para jugar
|
||||
section_t section; // Sección actual del programa
|
||||
};
|
||||
|
||||
extern options_t options;
|
||||
|
||||
// Crea e inicializa las opciones del programa
|
||||
void initOptions();
|
||||
|
||||
// Carga las opciones desde un fichero
|
||||
bool loadOptionsFromFile(const std::string &file_path);
|
||||
|
||||
// Guarda las opciones a un fichero
|
||||
bool saveOptionsToFile(const std::string &file_path);
|
||||
@@ -1,6 +1,10 @@
|
||||
#include "paleta.h"
|
||||
#include "gif.c"
|
||||
#include <stdio.h>
|
||||
#include <SDL2/SDL_pixels.h> // Para SDL_PIXELFORMAT_ARGB8888
|
||||
#include <SDL2/SDL_rect.h> // Para SDL_Rect
|
||||
#include <fcntl.h> // Para SEEK_END, SEEK_SET
|
||||
#include <stdio.h> // Para NULL, fseek, fclose, fopen, fread, ftell
|
||||
#include <stdlib.h> // Para malloc, free
|
||||
#include "gif.c" // Para LoadGif, LoadPalette
|
||||
|
||||
struct jSurface_s
|
||||
{
|
||||
@@ -169,7 +173,7 @@ bool pFadePal()
|
||||
{
|
||||
paleta[i] = paleta[i - 2];
|
||||
}
|
||||
|
||||
|
||||
paleta[1] = paleta[0];
|
||||
|
||||
return paleta[15] == paleta[0] ? true : false;
|
||||
@@ -1,5 +1,7 @@
|
||||
#pragma once
|
||||
#include <SDL2/SDL.h>
|
||||
|
||||
#include <SDL2/SDL_render.h> // Para SDL_Renderer
|
||||
#include <SDL2/SDL_stdinc.h> // Para Uint8, Uint32
|
||||
|
||||
typedef struct jSurface_s *jSurface;
|
||||
|
||||
@@ -8,7 +10,7 @@ void pDeleteSurface(jSurface surf);
|
||||
void pSetDest(jSurface surf);
|
||||
void pSetSource(jSurface surf);
|
||||
|
||||
jSurface pLoadSurface(const char* filename);
|
||||
jSurface pLoadSurface(const char *filename);
|
||||
|
||||
void pPutPixel(int x, int y, Uint8 color);
|
||||
Uint8 pGetPixel(int x, int y);
|
||||
@@ -1,21 +1,31 @@
|
||||
// IWYU pragma: no_include <bits/std_abs.h>
|
||||
#include "player.h"
|
||||
#include <fstream>
|
||||
#include <sstream>
|
||||
#include <stdlib.h> // Para rand
|
||||
#include <algorithm> // Para max, min
|
||||
#include <cmath> // Para ceil, abs
|
||||
#include "animatedsprite.h" // Para AnimatedSprite
|
||||
#include "asset.h" // Para Asset
|
||||
#include "const.h" // Para BORDER_TOP, BLOCK, BORDER_BOTTOM, BORDER...
|
||||
#include "debug.h" // Para Debug
|
||||
#include "input.h" // Para Input, inputs_e
|
||||
#include "jail_audio.h" // Para JA_LoadSound, JA_Sound_t, JA_PlaySound
|
||||
#include "resource.h" // Para Resource
|
||||
#include "room.h" // Para Room, tile_e
|
||||
#include "texture.h" // Para Texture
|
||||
#include "options.h"
|
||||
#include "screen.h"
|
||||
|
||||
// Constructor
|
||||
Player::Player(player_t player)
|
||||
: renderer_(Screen::get()->getRenderer()),
|
||||
input_(Input::get()),
|
||||
resource_(Resource::get()),
|
||||
asset_(Asset::get()),
|
||||
room_(player.room),
|
||||
debug_(Debug::get())
|
||||
{
|
||||
// Obten punteros a objetos
|
||||
this->resource = player.resource;
|
||||
this->asset = player.asset;
|
||||
this->renderer = player.renderer;
|
||||
this->input = player.input;
|
||||
this->room = player.room;
|
||||
this->debug = player.debug;
|
||||
this->options = player.options;
|
||||
|
||||
// Crea objetos
|
||||
sprite = new AnimatedSprite(renderer, resource->getAnimation(player.animation));
|
||||
sprite_ = new AnimatedSprite(renderer_, resource_->getAnimation(player.animation));
|
||||
|
||||
// Inicializa variables
|
||||
reLoadPalette();
|
||||
@@ -38,14 +48,14 @@ Player::Player(player_t player)
|
||||
h = 16;
|
||||
maxVY = 1.2f;
|
||||
|
||||
sprite->setPosX(player.spawn.x);
|
||||
sprite->setPosY(player.spawn.y);
|
||||
sprite->setWidth(8);
|
||||
sprite->setHeight(16);
|
||||
sprite_->setPosX(player.spawn.x);
|
||||
sprite_->setPosY(player.spawn.y);
|
||||
sprite_->setWidth(8);
|
||||
sprite_->setHeight(16);
|
||||
|
||||
sprite->setFlipH(player.spawn.flipH);
|
||||
sprite->setCurrentAnimation("walk");
|
||||
sprite->animate();
|
||||
sprite_->setFlipH(player.spawn.flipH);
|
||||
sprite_->setCurrentAnimation("walk");
|
||||
sprite_->animate();
|
||||
|
||||
lastPosition = getRect();
|
||||
colliderBox = getRect();
|
||||
@@ -53,45 +63,45 @@ Player::Player(player_t player)
|
||||
colliderPoints.insert(colliderPoints.end(), {p, p, p, p, p, p, p, p});
|
||||
underFeet.insert(underFeet.end(), {p, p});
|
||||
feet.insert(feet.end(), {p, p});
|
||||
jumpSound.push_back(JA_LoadSound(asset->get("jump1.wav").c_str()));
|
||||
jumpSound.push_back(JA_LoadSound(asset->get("jump2.wav").c_str()));
|
||||
jumpSound.push_back(JA_LoadSound(asset->get("jump3.wav").c_str()));
|
||||
jumpSound.push_back(JA_LoadSound(asset->get("jump4.wav").c_str()));
|
||||
jumpSound.push_back(JA_LoadSound(asset->get("jump5.wav").c_str()));
|
||||
jumpSound.push_back(JA_LoadSound(asset->get("jump6.wav").c_str()));
|
||||
jumpSound.push_back(JA_LoadSound(asset->get("jump7.wav").c_str()));
|
||||
jumpSound.push_back(JA_LoadSound(asset->get("jump8.wav").c_str()));
|
||||
jumpSound.push_back(JA_LoadSound(asset->get("jump9.wav").c_str()));
|
||||
jumpSound.push_back(JA_LoadSound(asset->get("jump10.wav").c_str()));
|
||||
jumpSound.push_back(JA_LoadSound(asset->get("jump11.wav").c_str()));
|
||||
jumpSound.push_back(JA_LoadSound(asset->get("jump12.wav").c_str()));
|
||||
jumpSound.push_back(JA_LoadSound(asset->get("jump13.wav").c_str()));
|
||||
jumpSound.push_back(JA_LoadSound(asset->get("jump14.wav").c_str()));
|
||||
jumpSound.push_back(JA_LoadSound(asset->get("jump15.wav").c_str()));
|
||||
jumpSound.push_back(JA_LoadSound(asset->get("jump16.wav").c_str()));
|
||||
jumpSound.push_back(JA_LoadSound(asset->get("jump17.wav").c_str()));
|
||||
jumpSound.push_back(JA_LoadSound(asset->get("jump18.wav").c_str()));
|
||||
jumpSound.push_back(JA_LoadSound(asset->get("jump19.wav").c_str()));
|
||||
jumpSound.push_back(JA_LoadSound(asset->get("jump20.wav").c_str()));
|
||||
jumpSound.push_back(JA_LoadSound(asset->get("jump21.wav").c_str()));
|
||||
jumpSound.push_back(JA_LoadSound(asset->get("jump22.wav").c_str()));
|
||||
jumpSound.push_back(JA_LoadSound(asset->get("jump23.wav").c_str()));
|
||||
jumpSound.push_back(JA_LoadSound(asset->get("jump24.wav").c_str()));
|
||||
jumpSound.push_back(JA_LoadSound(asset_->get("jump1.wav").c_str()));
|
||||
jumpSound.push_back(JA_LoadSound(asset_->get("jump2.wav").c_str()));
|
||||
jumpSound.push_back(JA_LoadSound(asset_->get("jump3.wav").c_str()));
|
||||
jumpSound.push_back(JA_LoadSound(asset_->get("jump4.wav").c_str()));
|
||||
jumpSound.push_back(JA_LoadSound(asset_->get("jump5.wav").c_str()));
|
||||
jumpSound.push_back(JA_LoadSound(asset_->get("jump6.wav").c_str()));
|
||||
jumpSound.push_back(JA_LoadSound(asset_->get("jump7.wav").c_str()));
|
||||
jumpSound.push_back(JA_LoadSound(asset_->get("jump8.wav").c_str()));
|
||||
jumpSound.push_back(JA_LoadSound(asset_->get("jump9.wav").c_str()));
|
||||
jumpSound.push_back(JA_LoadSound(asset_->get("jump10.wav").c_str()));
|
||||
jumpSound.push_back(JA_LoadSound(asset_->get("jump11.wav").c_str()));
|
||||
jumpSound.push_back(JA_LoadSound(asset_->get("jump12.wav").c_str()));
|
||||
jumpSound.push_back(JA_LoadSound(asset_->get("jump13.wav").c_str()));
|
||||
jumpSound.push_back(JA_LoadSound(asset_->get("jump14.wav").c_str()));
|
||||
jumpSound.push_back(JA_LoadSound(asset_->get("jump15.wav").c_str()));
|
||||
jumpSound.push_back(JA_LoadSound(asset_->get("jump16.wav").c_str()));
|
||||
jumpSound.push_back(JA_LoadSound(asset_->get("jump17.wav").c_str()));
|
||||
jumpSound.push_back(JA_LoadSound(asset_->get("jump18.wav").c_str()));
|
||||
jumpSound.push_back(JA_LoadSound(asset_->get("jump19.wav").c_str()));
|
||||
jumpSound.push_back(JA_LoadSound(asset_->get("jump20.wav").c_str()));
|
||||
jumpSound.push_back(JA_LoadSound(asset_->get("jump21.wav").c_str()));
|
||||
jumpSound.push_back(JA_LoadSound(asset_->get("jump22.wav").c_str()));
|
||||
jumpSound.push_back(JA_LoadSound(asset_->get("jump23.wav").c_str()));
|
||||
jumpSound.push_back(JA_LoadSound(asset_->get("jump24.wav").c_str()));
|
||||
|
||||
fallSound.push_back(JA_LoadSound(asset->get("jump11.wav").c_str()));
|
||||
fallSound.push_back(JA_LoadSound(asset->get("jump12.wav").c_str()));
|
||||
fallSound.push_back(JA_LoadSound(asset->get("jump13.wav").c_str()));
|
||||
fallSound.push_back(JA_LoadSound(asset->get("jump14.wav").c_str()));
|
||||
fallSound.push_back(JA_LoadSound(asset->get("jump15.wav").c_str()));
|
||||
fallSound.push_back(JA_LoadSound(asset->get("jump16.wav").c_str()));
|
||||
fallSound.push_back(JA_LoadSound(asset->get("jump17.wav").c_str()));
|
||||
fallSound.push_back(JA_LoadSound(asset->get("jump18.wav").c_str()));
|
||||
fallSound.push_back(JA_LoadSound(asset->get("jump19.wav").c_str()));
|
||||
fallSound.push_back(JA_LoadSound(asset->get("jump20.wav").c_str()));
|
||||
fallSound.push_back(JA_LoadSound(asset->get("jump21.wav").c_str()));
|
||||
fallSound.push_back(JA_LoadSound(asset->get("jump22.wav").c_str()));
|
||||
fallSound.push_back(JA_LoadSound(asset->get("jump23.wav").c_str()));
|
||||
fallSound.push_back(JA_LoadSound(asset->get("jump24.wav").c_str()));
|
||||
fallSound.push_back(JA_LoadSound(asset_->get("jump11.wav").c_str()));
|
||||
fallSound.push_back(JA_LoadSound(asset_->get("jump12.wav").c_str()));
|
||||
fallSound.push_back(JA_LoadSound(asset_->get("jump13.wav").c_str()));
|
||||
fallSound.push_back(JA_LoadSound(asset_->get("jump14.wav").c_str()));
|
||||
fallSound.push_back(JA_LoadSound(asset_->get("jump15.wav").c_str()));
|
||||
fallSound.push_back(JA_LoadSound(asset_->get("jump16.wav").c_str()));
|
||||
fallSound.push_back(JA_LoadSound(asset_->get("jump17.wav").c_str()));
|
||||
fallSound.push_back(JA_LoadSound(asset_->get("jump18.wav").c_str()));
|
||||
fallSound.push_back(JA_LoadSound(asset_->get("jump19.wav").c_str()));
|
||||
fallSound.push_back(JA_LoadSound(asset_->get("jump20.wav").c_str()));
|
||||
fallSound.push_back(JA_LoadSound(asset_->get("jump21.wav").c_str()));
|
||||
fallSound.push_back(JA_LoadSound(asset_->get("jump22.wav").c_str()));
|
||||
fallSound.push_back(JA_LoadSound(asset_->get("jump23.wav").c_str()));
|
||||
fallSound.push_back(JA_LoadSound(asset_->get("jump24.wav").c_str()));
|
||||
|
||||
jumpCounter = 0;
|
||||
fallCounter = 0;
|
||||
@@ -107,7 +117,7 @@ Player::Player(player_t player)
|
||||
// Destructor
|
||||
Player::~Player()
|
||||
{
|
||||
delete sprite;
|
||||
delete sprite_;
|
||||
|
||||
for (auto s : jumpSound)
|
||||
{
|
||||
@@ -118,38 +128,38 @@ Player::~Player()
|
||||
// Pinta el jugador en pantalla
|
||||
void Player::render()
|
||||
{
|
||||
sprite->getTexture()->setColor(color.r, color.g, color.b);
|
||||
sprite->render();
|
||||
sprite_->getTexture()->setColor(color.r, color.g, color.b);
|
||||
sprite_->render();
|
||||
|
||||
#ifdef DEBUG
|
||||
if (debug->getEnabled())
|
||||
if (debug_->getEnabled())
|
||||
{
|
||||
// Pinta los underfeet
|
||||
SDL_SetRenderDrawColor(renderer, 255, 0, 255, 255);
|
||||
SDL_RenderDrawPoint(renderer, underFeet[0].x, underFeet[0].y);
|
||||
SDL_RenderDrawPoint(renderer, underFeet[1].x, underFeet[1].y);
|
||||
SDL_SetRenderDrawColor(renderer_, 255, 0, 255, 255);
|
||||
SDL_RenderDrawPoint(renderer_, underFeet[0].x, underFeet[0].y);
|
||||
SDL_RenderDrawPoint(renderer_, underFeet[1].x, underFeet[1].y);
|
||||
|
||||
// Pinta rectangulo del jugador
|
||||
SDL_SetRenderDrawColor(renderer, debugColor.r, debugColor.g, debugColor.b, 192);
|
||||
SDL_SetRenderDrawColor(renderer_, debugColor.r, debugColor.g, debugColor.b, 192);
|
||||
SDL_Rect rect = getRect();
|
||||
SDL_RenderFillRect(renderer, &rect);
|
||||
SDL_SetRenderDrawColor(renderer, 0, 255, 255, 255);
|
||||
SDL_RenderDrawRect(renderer, &rect);
|
||||
SDL_RenderFillRect(renderer_, &rect);
|
||||
SDL_SetRenderDrawColor(renderer_, 0, 255, 255, 255);
|
||||
SDL_RenderDrawRect(renderer_, &rect);
|
||||
|
||||
// Pinta el rectangulo de movimiento
|
||||
SDL_SetRenderDrawColor(renderer, 255, 0, 0, 255);
|
||||
SDL_SetRenderDrawColor(renderer_, 255, 0, 0, 255);
|
||||
if (vx != 0.0f)
|
||||
{
|
||||
SDL_RenderFillRect(renderer, &rx);
|
||||
SDL_RenderFillRect(renderer_, &rx);
|
||||
}
|
||||
if (vy != 0.0f)
|
||||
{
|
||||
SDL_RenderFillRect(renderer, &ry);
|
||||
SDL_RenderFillRect(renderer_, &ry);
|
||||
}
|
||||
|
||||
// Pinta el punto de debug
|
||||
SDL_SetRenderDrawColor(renderer, rand() % 256, rand() % 256, rand() % 256, 255);
|
||||
SDL_RenderDrawPoint(renderer, debugPoint.x, debugPoint.y);
|
||||
SDL_SetRenderDrawColor(renderer_, rand() % 256, rand() % 256, rand() % 256, 255);
|
||||
SDL_RenderDrawPoint(renderer_, debugPoint.x, debugPoint.y);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
@@ -181,16 +191,16 @@ void Player::checkInput()
|
||||
|
||||
if (!autoMovement)
|
||||
{ // Comprueba las entradas de desplazamiento lateral solo en el caso de no estar enganchado a una superficie automatica
|
||||
if (input->checkInput(input_left))
|
||||
if (input_->checkInput(input_left))
|
||||
{
|
||||
vx = -0.6f;
|
||||
sprite->setFlipH(true);
|
||||
sprite_->setFlipH(true);
|
||||
}
|
||||
|
||||
else if (input->checkInput(input_right))
|
||||
else if (input_->checkInput(input_right))
|
||||
{
|
||||
vx = 0.6f;
|
||||
sprite->setFlipH(false);
|
||||
sprite_->setFlipH(false);
|
||||
}
|
||||
|
||||
else
|
||||
@@ -204,19 +214,19 @@ void Player::checkInput()
|
||||
}
|
||||
else
|
||||
{ // El movimiento lo proporciona la superficie
|
||||
vx = 0.6f * room->getAutoSurfaceDirection();
|
||||
vx = 0.6f * room_->getAutoSurfaceDirection();
|
||||
|
||||
if (vx > 0.0f)
|
||||
{
|
||||
sprite->setFlipH(false);
|
||||
sprite_->setFlipH(false);
|
||||
}
|
||||
else
|
||||
{
|
||||
sprite->setFlipH(true);
|
||||
sprite_->setFlipH(true);
|
||||
}
|
||||
}
|
||||
|
||||
if (input->checkInput(input_jump))
|
||||
if (input_->checkInput(input_jump))
|
||||
{
|
||||
// Solo puede saltar si ademas de estar (state == s_standing)
|
||||
// Esta sobre el suelo, rampa o suelo que se mueve
|
||||
@@ -398,7 +408,7 @@ void Player::move()
|
||||
#endif
|
||||
|
||||
// Comprueba la colisión con las superficies
|
||||
const int pos = room->checkRightSurfaces(&proj);
|
||||
const int pos = room_->checkRightSurfaces(&proj);
|
||||
|
||||
// Calcula la nueva posición
|
||||
if (pos == -1)
|
||||
@@ -414,7 +424,7 @@ void Player::move()
|
||||
if (state != s_jumping)
|
||||
{
|
||||
v_line_t leftSide = {(int)x, (int)y + h - 2, (int)y + h - 1}; // Comprueba solo los dos pixels de abajo
|
||||
const int ly = room->checkLeftSlopes(&leftSide);
|
||||
const int ly = room_->checkLeftSlopes(&leftSide);
|
||||
if (ly > -1)
|
||||
{
|
||||
y = ly - h;
|
||||
@@ -443,7 +453,7 @@ void Player::move()
|
||||
#endif
|
||||
|
||||
// Comprueba la colisión
|
||||
const int pos = room->checkLeftSurfaces(&proj);
|
||||
const int pos = room_->checkLeftSurfaces(&proj);
|
||||
|
||||
// Calcula la nueva posición
|
||||
if (pos == -1)
|
||||
@@ -459,7 +469,7 @@ void Player::move()
|
||||
if (state != s_jumping)
|
||||
{
|
||||
v_line_t rightSide = {(int)x + w - 1, (int)y + h - 2, (int)y + h - 1}; // Comprueba solo los dos pixels de abajo
|
||||
const int ry = room->checkRightSlopes(&rightSide);
|
||||
const int ry = room_->checkRightSlopes(&rightSide);
|
||||
if (ry > -1)
|
||||
{
|
||||
y = ry - h;
|
||||
@@ -504,7 +514,7 @@ void Player::move()
|
||||
#endif
|
||||
|
||||
// Comprueba la colisión
|
||||
const int pos = room->checkBottomSurfaces(&proj);
|
||||
const int pos = room_->checkBottomSurfaces(&proj);
|
||||
|
||||
// Calcula la nueva posición
|
||||
if (pos == -1)
|
||||
@@ -533,7 +543,7 @@ void Player::move()
|
||||
#endif
|
||||
|
||||
// Comprueba la colisión con las superficies normales y las automáticas
|
||||
const int pos = std::max(room->checkTopSurfaces(&proj), room->checkAutoSurfaces(&proj));
|
||||
const int pos = std::max(room_->checkTopSurfaces(&proj), room_->checkAutoSurfaces(&proj));
|
||||
if (pos > -1)
|
||||
{ // Si hay colisión lo mueve hasta donde no colisiona y pasa a estar sobre la superficie
|
||||
y = pos - h;
|
||||
@@ -548,7 +558,7 @@ void Player::move()
|
||||
{ // Las rampas no se miran si se está saltando
|
||||
v_line_t leftSide = {proj.x, proj.y, proj.y + proj.h - 1};
|
||||
v_line_t rightSide = {proj.x + proj.w - 1, proj.y, proj.y + proj.h - 1};
|
||||
const int p = std::max(room->checkRightSlopes(&rightSide), room->checkLeftSlopes(&leftSide));
|
||||
const int p = std::max(room_->checkRightSlopes(&rightSide), room_->checkLeftSlopes(&leftSide));
|
||||
if (p > -1)
|
||||
{ // No está saltando y hay colisión con una rampa
|
||||
// Calcula la nueva posición
|
||||
@@ -577,12 +587,12 @@ void Player::move()
|
||||
}
|
||||
|
||||
// Actualiza la posición del sprite
|
||||
sprite->setPosX(x);
|
||||
sprite->setPosY(y);
|
||||
sprite_->setPosX(x);
|
||||
sprite_->setPosY(y);
|
||||
|
||||
#ifdef DEBUG
|
||||
debug->add("RECT_X: " + std::to_string(rx.x) + "," + std::to_string(rx.y) + "," + std::to_string(rx.w) + "," + std::to_string(rx.h));
|
||||
debug->add("RECT_Y: " + std::to_string(ry.x) + "," + std::to_string(ry.y) + "," + std::to_string(ry.w) + "," + std::to_string(ry.h));
|
||||
debug_->add("RECT_X: " + std::to_string(rx.x) + "," + std::to_string(rx.y) + "," + std::to_string(rx.w) + "," + std::to_string(rx.h));
|
||||
debug_->add("RECT_Y: " + std::to_string(ry.x) + "," + std::to_string(ry.y) + "," + std::to_string(ry.w) + "," + std::to_string(ry.h));
|
||||
#endif
|
||||
}
|
||||
|
||||
@@ -591,7 +601,7 @@ void Player::animate()
|
||||
{
|
||||
if (vx != 0)
|
||||
{
|
||||
sprite->animate();
|
||||
sprite_->animate();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -621,7 +631,7 @@ void Player::playJumpSound()
|
||||
}
|
||||
|
||||
#ifdef DEBUG
|
||||
debug->add("JUMP: " + std::to_string(jumpCounter / 4));
|
||||
debug_->add("JUMP: " + std::to_string(jumpCounter / 4));
|
||||
#endif
|
||||
}
|
||||
|
||||
@@ -634,7 +644,7 @@ void Player::playFallSound()
|
||||
}
|
||||
|
||||
#ifdef DEBUG
|
||||
debug->add("FALL: " + std::to_string(fallCounter / 4));
|
||||
debug_->add("FALL: " + std::to_string(fallCounter / 4));
|
||||
#endif
|
||||
}
|
||||
|
||||
@@ -650,28 +660,28 @@ bool Player::isOnFloor()
|
||||
// Comprueba las superficies
|
||||
for (auto f : underFeet)
|
||||
{
|
||||
onFloor |= room->checkTopSurfaces(&f);
|
||||
onFloor |= room->checkAutoSurfaces(&f);
|
||||
onFloor |= room_->checkTopSurfaces(&f);
|
||||
onFloor |= room_->checkAutoSurfaces(&f);
|
||||
}
|
||||
|
||||
// Comprueba las rampas
|
||||
onSlopeL = room->checkLeftSlopes(&underFeet[0]);
|
||||
onSlopeR = room->checkRightSlopes(&underFeet[1]);
|
||||
onSlopeL = room_->checkLeftSlopes(&underFeet[0]);
|
||||
onSlopeR = room_->checkRightSlopes(&underFeet[1]);
|
||||
|
||||
#ifdef DEBUG
|
||||
if (onFloor)
|
||||
{
|
||||
debug->add("ON_FLOOR");
|
||||
debug_->add("ON_FLOOR");
|
||||
}
|
||||
|
||||
if (onSlopeL)
|
||||
{
|
||||
debug->add("ON_SLOPE_L: " + std::to_string(underFeet[0].x) + "," + std::to_string(underFeet[0].y));
|
||||
debug_->add("ON_SLOPE_L: " + std::to_string(underFeet[0].x) + "," + std::to_string(underFeet[0].y));
|
||||
}
|
||||
|
||||
if (onSlopeR)
|
||||
{
|
||||
debug->add("ON_SLOPE_R: " + std::to_string(underFeet[1].x) + "," + std::to_string(underFeet[1].y));
|
||||
debug_->add("ON_SLOPE_R: " + std::to_string(underFeet[1].x) + "," + std::to_string(underFeet[1].y));
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -688,13 +698,13 @@ bool Player::isOnAutoSurface()
|
||||
// Comprueba las superficies
|
||||
for (auto f : underFeet)
|
||||
{
|
||||
onAutoSurface |= room->checkAutoSurfaces(&f);
|
||||
onAutoSurface |= room_->checkAutoSurfaces(&f);
|
||||
}
|
||||
|
||||
#ifdef DEBUG
|
||||
if (onAutoSurface)
|
||||
{
|
||||
debug->add("ON_AUTO_SURFACE");
|
||||
debug_->add("ON_AUTO_SURFACE");
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -714,13 +724,13 @@ bool Player::isOnDownSlope()
|
||||
underFeet[1].y += 1;
|
||||
|
||||
// Comprueba las rampas
|
||||
onSlope |= room->checkLeftSlopes(&underFeet[0]);
|
||||
onSlope |= room->checkRightSlopes(&underFeet[1]);
|
||||
onSlope |= room_->checkLeftSlopes(&underFeet[0]);
|
||||
onSlope |= room_->checkRightSlopes(&underFeet[1]);
|
||||
|
||||
#ifdef DEBUG
|
||||
if (onSlope)
|
||||
{
|
||||
debug->add("ON_DOWN_SLOPE");
|
||||
debug_->add("ON_DOWN_SLOPE");
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -738,7 +748,7 @@ bool Player::checkKillingTiles()
|
||||
|
||||
for (auto c : colliderPoints)
|
||||
{
|
||||
check |= (room->getTile(c) == t_kill);
|
||||
check |= (room_->getTile(c) == t_kill);
|
||||
}
|
||||
|
||||
// Mata al jugador si hay colisión
|
||||
@@ -761,7 +771,7 @@ playerSpawn_t Player::getSpawnParams()
|
||||
params.vy = vy;
|
||||
params.jumpIni = jumpIni;
|
||||
params.state = state;
|
||||
params.flipH = sprite->getFlipH();
|
||||
params.flipH = sprite_->getFlipH();
|
||||
|
||||
return params;
|
||||
}
|
||||
@@ -769,27 +779,27 @@ playerSpawn_t Player::getSpawnParams()
|
||||
// Recarga la textura
|
||||
void Player::reLoadTexture()
|
||||
{
|
||||
sprite->getTexture()->reLoad();
|
||||
sprite_->getTexture()->reLoad();
|
||||
}
|
||||
|
||||
// Recarga la paleta
|
||||
void Player::reLoadPalette()
|
||||
{
|
||||
color = stringToColor(options->palette, "white");
|
||||
if (options->cheat.infiniteLives)
|
||||
color = stringToColor(options.palette, "white");
|
||||
if (options.cheat.infiniteLives)
|
||||
{
|
||||
color = stringToColor(options->palette, "yellow");
|
||||
color = stringToColor(options.palette, "yellow");
|
||||
}
|
||||
if (options->cheat.invincible)
|
||||
if (options.cheat.invincible)
|
||||
{
|
||||
color = stringToColor(options->palette, "cyan");
|
||||
color = stringToColor(options.palette, "cyan");
|
||||
}
|
||||
}
|
||||
|
||||
// Establece el valor de la variable
|
||||
void Player::setRoom(Room *room)
|
||||
{
|
||||
this->room = room;
|
||||
this->room_ = room;
|
||||
}
|
||||
|
||||
// Actualiza los puntos de colisión
|
||||
|
||||
@@ -1,19 +1,17 @@
|
||||
#pragma once
|
||||
|
||||
#include <SDL2/SDL.h>
|
||||
#include "jail_engine/animatedsprite.h"
|
||||
#include "jail_engine/asset.h"
|
||||
#include "jail_engine/debug.h"
|
||||
#include "jail_engine/input.h"
|
||||
#include "jail_engine/resource.h"
|
||||
#include "jail_engine/utils.h"
|
||||
#include "const.h"
|
||||
#include "room.h"
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#ifndef PLAYER_H
|
||||
#define PLAYER_H
|
||||
#include <SDL2/SDL_rect.h> // Para SDL_Rect, SDL_Point
|
||||
#include <SDL2/SDL_render.h> // Para SDL_Renderer
|
||||
#include <string> // Para basic_string, string
|
||||
#include <vector> // Para vector
|
||||
#include "utils.h" // Para color_t
|
||||
class AnimatedSprite;
|
||||
class Asset;
|
||||
class Debug;
|
||||
class Input;
|
||||
class Resource;
|
||||
class Room;
|
||||
struct JA_Sound_t;
|
||||
|
||||
enum state_e
|
||||
{
|
||||
@@ -38,27 +36,20 @@ struct player_t
|
||||
playerSpawn_t spawn;
|
||||
std::string png;
|
||||
std::string animation;
|
||||
SDL_Renderer *renderer;
|
||||
Resource *resource;
|
||||
Asset *asset;
|
||||
options_t *options;
|
||||
Input *input;
|
||||
Room *room;
|
||||
Debug *debug;
|
||||
};
|
||||
|
||||
class Player
|
||||
{
|
||||
public:
|
||||
// Objetos y punteros
|
||||
SDL_Renderer *renderer; // El renderizador de la ventana
|
||||
Input *input; // Objeto para gestionar la entrada
|
||||
Resource *resource; // Objeto con los recursos
|
||||
Asset *asset; // Objeto con la ruta a todos los ficheros de recursos
|
||||
Room *room; // Objeto encargado de gestionar cada habitación del juego
|
||||
AnimatedSprite *sprite; // Sprite del enemigo
|
||||
Debug *debug; // Objeto para gestionar la información de debug
|
||||
options_t *options; // Puntero a las opciones del juego
|
||||
SDL_Renderer *renderer_; // El renderizador de la ventana
|
||||
Input *input_; // Objeto para gestionar la entrada
|
||||
Resource *resource_; // Objeto con los recursos
|
||||
Asset *asset_; // Objeto con la ruta a todos los ficheros de recursos
|
||||
Room *room_; // Objeto encargado de gestionar cada habitación del juego
|
||||
AnimatedSprite *sprite_; // Sprite del jugador
|
||||
Debug *debug_; // Objeto para gestionar la información de debug
|
||||
|
||||
// Variables
|
||||
float x; // Posición del jugador en el eje X
|
||||
@@ -81,8 +72,8 @@ public:
|
||||
SDL_Rect lastPosition; // Contiene la ultima posición del jugador, por si hay que deshacer algun movimiento
|
||||
int jumpIni; // Valor del eje Y en el que se inicia el salto
|
||||
float maxVY; // Velocidad máxima que puede alcanzar al desplazarse en vertical
|
||||
std::vector<JA_Sound_t*> jumpSound; // Vecor con todos los sonidos del salto
|
||||
std::vector<JA_Sound_t*> fallSound; // Vecor con todos los sonidos de la caída
|
||||
std::vector<JA_Sound_t *> jumpSound; // Vecor con todos los sonidos del salto
|
||||
std::vector<JA_Sound_t *> fallSound; // Vecor con todos los sonidos de la caída
|
||||
int jumpCounter; // Cuenta el tiempo de salto
|
||||
int fallCounter; // Cuenta el tiempo de caida
|
||||
bool alive; // Indica si el jugador esta vivo o no
|
||||
@@ -194,6 +185,4 @@ public:
|
||||
|
||||
// Quita el modo pausa del jugador
|
||||
void resume();
|
||||
};
|
||||
|
||||
#endif
|
||||
};
|
||||
@@ -1,12 +1,35 @@
|
||||
#include "resource.h"
|
||||
#include <iostream>
|
||||
#include <iostream> // Para basic_ostream, operator<<, cout, endl
|
||||
#include "animatedsprite.h" // Para animatedSprite_t, loadAnimationFromFile
|
||||
#include "asset.h" // Para Asset
|
||||
#include "enemy.h" // Para enemy_t
|
||||
#include "item.h" // Para item_t
|
||||
#include "room.h" // Para room_t, loadRoomFile, loadRoomTileFile
|
||||
#include "text.h" // Para textFile_t, LoadTextFile
|
||||
#include "texture.h" // Para Texture
|
||||
#include "utils.h" // Para options_t
|
||||
#include "screen.h"
|
||||
#include "options.h"
|
||||
|
||||
// Constructor
|
||||
Resource::Resource(SDL_Renderer *renderer, Asset *asset, options_t *options)
|
||||
// [SINGLETON]
|
||||
Resource *Resource::resource_ = nullptr;
|
||||
|
||||
// [SINGLETON] Crearemos el objeto con esta función estática
|
||||
void Resource::init()
|
||||
{
|
||||
this->renderer = renderer;
|
||||
this->asset = asset;
|
||||
this->options = options;
|
||||
Resource::resource_ = new Resource();
|
||||
}
|
||||
|
||||
// [SINGLETON] Destruiremos el objeto con esta función estática
|
||||
void Resource::destroy()
|
||||
{
|
||||
delete Resource::resource_;
|
||||
}
|
||||
|
||||
// [SINGLETON] Con este método obtenemos el objeto y podemos trabajar con él
|
||||
Resource *Resource::get()
|
||||
{
|
||||
return Resource::resource_;
|
||||
}
|
||||
|
||||
// Carga las texturas de una lista
|
||||
@@ -14,23 +37,23 @@ void Resource::loadTextures(std::vector<std::string> list)
|
||||
{
|
||||
for (auto l : list)
|
||||
{
|
||||
if (options->console)
|
||||
if (options.console)
|
||||
{
|
||||
std::cout << "\nLOAD TEXTURE: " << l << std::endl;
|
||||
std::cout << "png: " << asset->get(l) << std::endl;
|
||||
std::cout << "png: " << Asset::get()->get(l) << std::endl;
|
||||
}
|
||||
|
||||
res_texture_t t;
|
||||
t.name = l;
|
||||
t.texture = new Texture(renderer, asset->get(t.name), options->console);
|
||||
textures.push_back(t);
|
||||
t.texture = new Texture(Screen::get()->getRenderer(), Asset::get()->get(t.name), options.console);
|
||||
textures_.push_back(t);
|
||||
}
|
||||
}
|
||||
|
||||
// Vuelve a cargar las texturas
|
||||
void Resource::reLoadTextures()
|
||||
{
|
||||
for (auto texture : textures)
|
||||
for (auto texture : textures_)
|
||||
{
|
||||
texture.texture->reLoad();
|
||||
}
|
||||
@@ -44,17 +67,17 @@ void Resource::loadAnimations(std::vector<std::string> list)
|
||||
// Extrae el nombre del fichero sin la extension para crear el nombre del fichero de la textura
|
||||
const std::string pngFile = l.substr(0, l.find_last_of(".")) + ".png";
|
||||
|
||||
if (options->console)
|
||||
if (options.console)
|
||||
{
|
||||
std::cout << "\nLOAD ANIMATION: " << l << std::endl;
|
||||
std::cout << "png: " << asset->get(pngFile) << std::endl;
|
||||
std::cout << "ani: " << asset->get(l) << std::endl;
|
||||
std::cout << "png: " << Asset::get()->get(pngFile) << std::endl;
|
||||
std::cout << "ani: " << Asset::get()->get(l) << std::endl;
|
||||
}
|
||||
|
||||
res_animation_t as;
|
||||
as.name = l;
|
||||
as.animation = new animatedSprite_t(loadAnimationFromFile(getTexture(pngFile), asset->get(as.name), options->console));
|
||||
animations.push_back(as);
|
||||
as.animation = new animatedSprite_t(loadAnimationFromFile(getTexture(pngFile), Asset::get()->get(as.name), options.console));
|
||||
animations_.push_back(as);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -63,12 +86,12 @@ void Resource::reLoadAnimations()
|
||||
{
|
||||
// reLoadTextures();
|
||||
|
||||
for (auto &a : animations)
|
||||
for (auto &a : animations_)
|
||||
{
|
||||
// Extrae el nombre del fichero sin la extension para crear el nombre del fichero de la textura
|
||||
const std::string pngFile = a.name.substr(0, a.name.find_last_of(".")) + ".png";
|
||||
delete a.animation;
|
||||
a.animation = new animatedSprite_t(loadAnimationFromFile(getTexture(pngFile), asset->get(a.name), options->console));
|
||||
a.animation = new animatedSprite_t(loadAnimationFromFile(getTexture(pngFile), Asset::get()->get(a.name), options.console));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -79,18 +102,18 @@ void Resource::loadOffsets(std::vector<std::string> list)
|
||||
{
|
||||
res_textOffset_t to;
|
||||
to.name = l;
|
||||
to.textFile = new textFile_t(LoadTextFile(asset->get(l), options->console));
|
||||
offsets.push_back(to);
|
||||
to.textFile = new textFile_t(LoadTextFile(Asset::get()->get(l), options.console));
|
||||
offsets_.push_back(to);
|
||||
}
|
||||
}
|
||||
|
||||
// Vuelve a cargar los offsets
|
||||
void Resource::reLoadOffsets()
|
||||
{
|
||||
for (auto &o : offsets)
|
||||
for (auto &o : offsets_)
|
||||
{
|
||||
delete o.textFile;
|
||||
o.textFile = new textFile_t(LoadTextFile(asset->get(o.name), options->console));
|
||||
o.textFile = new textFile_t(LoadTextFile(Asset::get()->get(o.name), options.console));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -101,18 +124,18 @@ void Resource::loadTileMaps(std::vector<std::string> list)
|
||||
{
|
||||
res_tileMap_t tm;
|
||||
tm.name = l;
|
||||
tm.tileMap = new std::vector<int>(loadRoomTileFile(asset->get(l), options->console));
|
||||
tileMaps.push_back(tm);
|
||||
tm.tileMap = new std::vector<int>(loadRoomTileFile(Asset::get()->get(l), options.console));
|
||||
tile_maps_.push_back(tm);
|
||||
}
|
||||
}
|
||||
|
||||
// Vuelve a cargar los mapas de tiles
|
||||
void Resource::reLoadTileMaps()
|
||||
{
|
||||
for (auto &tm : tileMaps)
|
||||
for (auto &tm : tile_maps_)
|
||||
{
|
||||
delete tm.tileMap;
|
||||
tm.tileMap = new std::vector<int>(loadRoomTileFile(asset->get(tm.name), options->console));
|
||||
tm.tileMap = new std::vector<int>(loadRoomTileFile(Asset::get()->get(tm.name), options.console));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -123,7 +146,7 @@ void Resource::loadRooms(std::vector<std::string> list)
|
||||
{
|
||||
res_room_t r;
|
||||
r.name = l;
|
||||
r.room = new room_t(loadRoomFile(asset->get(l), options->console));
|
||||
r.room = new room_t(loadRoomFile(Asset::get()->get(l), options.console));
|
||||
r.room->tileMap = getTileMap(r.room->tileMapFile);
|
||||
for (auto &e : r.room->enemies)
|
||||
{
|
||||
@@ -135,7 +158,7 @@ void Resource::loadRooms(std::vector<std::string> list)
|
||||
}
|
||||
r.room->textureA = getTexture("standard.png");
|
||||
r.room->textureB = getTexture("standard_zxarne.png");
|
||||
rooms.push_back(r);
|
||||
rooms_.push_back(r);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -144,10 +167,10 @@ void Resource::reLoadRooms()
|
||||
{
|
||||
reLoadTileMaps();
|
||||
|
||||
for (auto &r : rooms)
|
||||
for (auto &r : rooms_)
|
||||
{
|
||||
delete r.room;
|
||||
r.room = new room_t(loadRoomFile(asset->get(r.name)));
|
||||
r.room = new room_t(loadRoomFile(Asset::get()->get(r.name)));
|
||||
r.room->tileMap = getTileMap(r.room->tileMapFile);
|
||||
for (auto &e : r.room->enemies)
|
||||
{
|
||||
@@ -173,51 +196,51 @@ void Resource::reLoad()
|
||||
// Libera las texturas
|
||||
void Resource::freeTextures()
|
||||
{
|
||||
for (auto texture : textures)
|
||||
for (auto texture : textures_)
|
||||
{
|
||||
delete texture.texture;
|
||||
}
|
||||
textures.clear();
|
||||
textures_.clear();
|
||||
}
|
||||
|
||||
// Libera las animaciones
|
||||
void Resource::freeAnimations()
|
||||
{
|
||||
for (auto a : animations)
|
||||
for (auto a : animations_)
|
||||
{
|
||||
delete a.animation;
|
||||
}
|
||||
animations.clear();
|
||||
animations_.clear();
|
||||
}
|
||||
|
||||
// Libera los offsets
|
||||
void Resource::freeOffsets()
|
||||
{
|
||||
for (auto o : offsets)
|
||||
for (auto o : offsets_)
|
||||
{
|
||||
delete o.textFile;
|
||||
}
|
||||
offsets.clear();
|
||||
offsets_.clear();
|
||||
}
|
||||
|
||||
// Libera los mapas de tiles
|
||||
void Resource::freeTileMaps()
|
||||
{
|
||||
for (auto t : tileMaps)
|
||||
for (auto t : tile_maps_)
|
||||
{
|
||||
delete t.tileMap;
|
||||
}
|
||||
tileMaps.clear();
|
||||
tile_maps_.clear();
|
||||
}
|
||||
|
||||
// Libera las habitaciones
|
||||
void Resource::freeRooms()
|
||||
{
|
||||
for (auto r : rooms)
|
||||
for (auto r : rooms_)
|
||||
{
|
||||
delete r.room;
|
||||
}
|
||||
rooms.clear();
|
||||
rooms_.clear();
|
||||
}
|
||||
|
||||
// Libera todos los recursos
|
||||
@@ -233,7 +256,7 @@ void Resource::free()
|
||||
// Obtiene una textura
|
||||
Texture *Resource::getTexture(std::string name)
|
||||
{
|
||||
for (auto texture : textures)
|
||||
for (auto texture : textures_)
|
||||
{
|
||||
// if (texture.name.find(name) != std::string::npos)
|
||||
if (texture.name == name)
|
||||
@@ -245,7 +268,7 @@ Texture *Resource::getTexture(std::string name)
|
||||
}
|
||||
}
|
||||
|
||||
if (options->console)
|
||||
if (options.console)
|
||||
{
|
||||
std::cout << "NOT FOUND ON CACHE: " << name << std::endl;
|
||||
}
|
||||
@@ -255,7 +278,7 @@ Texture *Resource::getTexture(std::string name)
|
||||
// Obtiene una animación
|
||||
animatedSprite_t *Resource::getAnimation(std::string name)
|
||||
{
|
||||
for (auto animation : animations)
|
||||
for (auto animation : animations_)
|
||||
{
|
||||
// if (animation.name.find(name) != std::string::npos)
|
||||
if (animation.name == name)
|
||||
@@ -266,7 +289,7 @@ animatedSprite_t *Resource::getAnimation(std::string name)
|
||||
}
|
||||
}
|
||||
|
||||
if (options->console)
|
||||
if (options.console)
|
||||
{
|
||||
std::cout << "NOT FOUND ON CACHE: " << name << std::endl;
|
||||
}
|
||||
@@ -276,7 +299,7 @@ animatedSprite_t *Resource::getAnimation(std::string name)
|
||||
// Obtiene un offset
|
||||
textFile_t *Resource::getOffset(std::string name)
|
||||
{
|
||||
for (auto offset : offsets)
|
||||
for (auto offset : offsets_)
|
||||
{
|
||||
// if (offset.name.find(name) != std::string::npos)
|
||||
if (offset.name == name)
|
||||
@@ -285,7 +308,7 @@ textFile_t *Resource::getOffset(std::string name)
|
||||
}
|
||||
}
|
||||
|
||||
if (options->console)
|
||||
if (options.console)
|
||||
{
|
||||
std::cout << "NOT FOUND ON CACHE: " << name << std::endl;
|
||||
}
|
||||
@@ -295,7 +318,7 @@ textFile_t *Resource::getOffset(std::string name)
|
||||
// Obtiene un mapa de tiles
|
||||
std::vector<int> *Resource::getTileMap(std::string name)
|
||||
{
|
||||
for (auto tileMap : tileMaps)
|
||||
for (auto tileMap : tile_maps_)
|
||||
{
|
||||
// if (tileMap.name.find(name) != std::string::npos)
|
||||
if (tileMap.name == name)
|
||||
@@ -304,7 +327,7 @@ std::vector<int> *Resource::getTileMap(std::string name)
|
||||
}
|
||||
}
|
||||
|
||||
if (options->console)
|
||||
if (options.console)
|
||||
{
|
||||
std::cout << "NOT FOUND ON CACHE: " << name << std::endl;
|
||||
}
|
||||
@@ -314,7 +337,7 @@ std::vector<int> *Resource::getTileMap(std::string name)
|
||||
// Obtiene una habitacion
|
||||
room_t *Resource::getRoom(std::string name)
|
||||
{
|
||||
for (auto room : rooms)
|
||||
for (auto room : rooms_)
|
||||
{
|
||||
// if (room.name.find(name) != std::string::npos)
|
||||
if (room.name == name)
|
||||
@@ -323,7 +346,7 @@ room_t *Resource::getRoom(std::string name)
|
||||
}
|
||||
}
|
||||
|
||||
if (options->console)
|
||||
if (options.console)
|
||||
{
|
||||
std::cout << "NOT FOUND ON CACHE: " << name << std::endl;
|
||||
}
|
||||
@@ -333,5 +356,5 @@ room_t *Resource::getRoom(std::string name)
|
||||
// Obtiene todas las habitaciones
|
||||
std::vector<res_room_t> *Resource::getAllRooms()
|
||||
{
|
||||
return &rooms;
|
||||
return &rooms_;
|
||||
}
|
||||
@@ -1,17 +1,14 @@
|
||||
#pragma once
|
||||
|
||||
#include <SDL2/SDL.h>
|
||||
#include "animatedsprite.h"
|
||||
#include "asset.h"
|
||||
#include "../room.h"
|
||||
#include "text.h"
|
||||
#include "texture.h"
|
||||
#include "utils.h"
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#ifndef RESOURCE_H
|
||||
#define RESOURCE_H
|
||||
#include <SDL2/SDL_render.h> // Para SDL_Renderer
|
||||
#include <string> // Para string, basic_string
|
||||
#include <vector> // Para vector
|
||||
class Asset;
|
||||
class Texture;
|
||||
struct animatedSprite_t;
|
||||
struct options_t;
|
||||
struct room_t;
|
||||
struct textFile_t;
|
||||
|
||||
struct res_texture_t
|
||||
{
|
||||
@@ -47,21 +44,31 @@ struct res_room_t
|
||||
class Resource
|
||||
{
|
||||
private:
|
||||
// Objetos y punteros
|
||||
SDL_Renderer *renderer; // El renderizador de la ventana
|
||||
Asset *asset; // Objeto con la ruta a todos los ficheros de recursos
|
||||
options_t *options; // Puntero a las opciones del juego
|
||||
// [SINGLETON] Objeto privado
|
||||
static Resource *resource_;
|
||||
|
||||
// Variables
|
||||
std::vector<res_texture_t> textures;
|
||||
std::vector<res_animation_t> animations;
|
||||
std::vector<res_textOffset_t> offsets;
|
||||
std::vector<res_tileMap_t> tileMaps;
|
||||
std::vector<res_room_t> rooms;
|
||||
std::vector<res_texture_t> textures_;
|
||||
std::vector<res_animation_t> animations_;
|
||||
std::vector<res_textOffset_t> offsets_;
|
||||
std::vector<res_tileMap_t> tile_maps_;
|
||||
std::vector<res_room_t> rooms_;
|
||||
|
||||
// Constructor
|
||||
Resource() = default;
|
||||
|
||||
// Destructor
|
||||
~Resource() = default;
|
||||
|
||||
public:
|
||||
// Constructor
|
||||
Resource(SDL_Renderer *renderer, Asset *asset, options_t *options);
|
||||
// [SINGLETON] Crearemos el objeto con esta función estática
|
||||
static void init();
|
||||
|
||||
// [SINGLETON] Destruiremos el objeto con esta función estática
|
||||
static void destroy();
|
||||
|
||||
// [SINGLETON] Con este método obtenemos el objeto y podemos trabajar con él
|
||||
static Resource *get();
|
||||
|
||||
// Carga las texturas de una lista
|
||||
void loadTextures(std::vector<std::string> list);
|
||||
@@ -131,6 +138,4 @@ public:
|
||||
|
||||
// Obtiene todas las habitaciones
|
||||
std::vector<res_room_t> *getAllRooms();
|
||||
};
|
||||
|
||||
#endif
|
||||
};
|
||||
@@ -1,7 +1,20 @@
|
||||
#include "room.h"
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <sstream>
|
||||
#include <SDL2/SDL_blendmode.h> // Para SDL_BLENDMODE_BLEND
|
||||
#include <SDL2/SDL_error.h> // Para SDL_GetError
|
||||
#include <SDL2/SDL_pixels.h> // Para SDL_PIXELFORMAT_RGBA8888
|
||||
#include <stdlib.h> // Para rand
|
||||
#include <fstream> // Para basic_ostream, operator<<, basic_ist...
|
||||
#include <iostream> // Para cout
|
||||
#include <sstream> // Para basic_stringstream
|
||||
#include "asset.h" // Para Asset
|
||||
#include "const.h" // Para BLOCK, PLAY_AREA_HEIGHT, PLAY_AREA_W...
|
||||
#include "debug.h" // Para Debug
|
||||
#include "item_tracker.h" // Para ItemTracker
|
||||
#include "jail_audio.h" // Para JA_DeleteSound, JA_LoadSound, JA_Pla...
|
||||
#include "screen.h" // Para Screen
|
||||
#include "sprite.h" // Para Sprite
|
||||
#include "texture.h" // Para Texture
|
||||
#include "options.h"
|
||||
|
||||
// Carga las variables y texturas desde un fichero de mapa de tiles
|
||||
std::vector<int> loadRoomTileFile(std::string file_path, bool verbose)
|
||||
@@ -391,17 +404,14 @@ bool setItem(item_t *item, std::string var, std::string value)
|
||||
}
|
||||
|
||||
// Constructor
|
||||
Room::Room(room_t *room, SDL_Renderer *renderer, Screen *screen, Asset *asset, options_t *options, ItemTracker *itemTracker, int *itemsPicked, bool jailEnabled, Debug *debug)
|
||||
Room::Room(room_t *room, ItemTracker *itemTracker, int *itemsPicked, bool jailEnabled)
|
||||
: screen(Screen::get()),
|
||||
renderer(Screen::get()->getRenderer()),
|
||||
asset(Asset::get()),
|
||||
debug(Debug::get()),
|
||||
itemTracker(itemTracker),
|
||||
itemsPicked(itemsPicked)
|
||||
{
|
||||
// Copia los punteros a objetos
|
||||
this->renderer = renderer;
|
||||
this->asset = asset;
|
||||
this->screen = screen;
|
||||
this->itemTracker = itemTracker;
|
||||
this->itemsPicked = itemsPicked;
|
||||
this->debug = debug;
|
||||
this->options = options;
|
||||
|
||||
number = room->number;
|
||||
name = room->name;
|
||||
bgColor = room->bgColor;
|
||||
@@ -418,7 +428,7 @@ Room::Room(room_t *room, SDL_Renderer *renderer, Screen *screen, Asset *asset, o
|
||||
textureA = room->textureA;
|
||||
textureB = room->textureB;
|
||||
tileMap = *room->tileMap;
|
||||
texture = (options->palette == p_zxspectrum) ? textureA : textureB;
|
||||
texture = (options.palette == p_zxspectrum) ? textureA : textureB;
|
||||
this->jailEnabled = jailEnabled;
|
||||
|
||||
// Inicializa variables
|
||||
@@ -433,7 +443,7 @@ Room::Room(room_t *room, SDL_Renderer *renderer, Screen *screen, Asset *asset, o
|
||||
for (auto &enemy : room->enemies)
|
||||
{
|
||||
enemy.renderer = renderer;
|
||||
enemy.palette = options->palette;
|
||||
enemy.palette = options.palette;
|
||||
enemies.push_back(new Enemy(enemy));
|
||||
}
|
||||
|
||||
@@ -445,8 +455,8 @@ Room::Room(room_t *room, SDL_Renderer *renderer, Screen *screen, Asset *asset, o
|
||||
if (!itemTracker->hasBeenPicked(room->name, itemPos))
|
||||
{
|
||||
item.renderer = renderer;
|
||||
item.color1 = stringToColor(options->palette, itemColor1);
|
||||
item.color2 = stringToColor(options->palette, itemColor2);
|
||||
item.color1 = stringToColor(options.palette, itemColor1);
|
||||
item.color2 = stringToColor(options.palette, itemColor2);
|
||||
items.push_back(new Item(item));
|
||||
}
|
||||
}
|
||||
@@ -476,7 +486,7 @@ Room::Room(room_t *room, SDL_Renderer *renderer, Screen *screen, Asset *asset, o
|
||||
mapTexture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, PLAY_AREA_WIDTH, PLAY_AREA_HEIGHT);
|
||||
if (mapTexture == nullptr)
|
||||
{
|
||||
if (options->console)
|
||||
if (options.console)
|
||||
{
|
||||
std::cout << "Error: mapTexture could not be created!\nSDL Error: " << SDL_GetError() << std::endl;
|
||||
}
|
||||
@@ -487,7 +497,7 @@ Room::Room(room_t *room, SDL_Renderer *renderer, Screen *screen, Asset *asset, o
|
||||
fillMapTexture();
|
||||
|
||||
// Establece el color del borde
|
||||
screen->setBorderColor(stringToColor(options->palette, room->borderColor));
|
||||
screen->setBorderColor(stringToColor(options.palette, room->borderColor));
|
||||
}
|
||||
|
||||
// Destructor
|
||||
@@ -522,19 +532,19 @@ std::string Room::getName()
|
||||
// Devuelve el color de la habitación
|
||||
color_t Room::getBGColor()
|
||||
{
|
||||
return stringToColor(options->palette, bgColor);
|
||||
return stringToColor(options.palette, bgColor);
|
||||
}
|
||||
|
||||
// Devuelve el color del borde
|
||||
color_t Room::getBorderColor()
|
||||
{
|
||||
return stringToColor(options->palette, borderColor);
|
||||
return stringToColor(options.palette, borderColor);
|
||||
}
|
||||
|
||||
// Crea la textura con el mapeado de la habitación
|
||||
void Room::fillMapTexture()
|
||||
{
|
||||
const color_t color = stringToColor(options->palette, bgColor);
|
||||
const color_t color = stringToColor(options.palette, bgColor);
|
||||
SDL_SetRenderTarget(renderer, mapTexture);
|
||||
SDL_SetRenderDrawColor(renderer, color.r, color.g, color.b, 0xFF);
|
||||
SDL_RenderClear(renderer);
|
||||
@@ -829,7 +839,7 @@ bool Room::itemCollision(SDL_Rect &rect)
|
||||
items.erase(items.begin() + i);
|
||||
JA_PlaySound(itemSound);
|
||||
*itemsPicked = *itemsPicked + 1;
|
||||
options->stats.items = *itemsPicked;
|
||||
options.stats.items = *itemsPicked;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -860,20 +870,20 @@ void Room::reLoadPalette()
|
||||
// Cambia el color de los items
|
||||
for (auto item : items)
|
||||
{
|
||||
item->setColors(stringToColor(options->palette, itemColor1), stringToColor(options->palette, itemColor2));
|
||||
item->setColors(stringToColor(options.palette, itemColor1), stringToColor(options.palette, itemColor2));
|
||||
}
|
||||
|
||||
// Cambia el color de los enemigos
|
||||
for (auto enemy : enemies)
|
||||
{
|
||||
enemy->setPalette(options->palette);
|
||||
enemy->setPalette(options.palette);
|
||||
}
|
||||
|
||||
// Establece el color del borde
|
||||
screen->setBorderColor(stringToColor(options->palette, borderColor));
|
||||
screen->setBorderColor(stringToColor(options.palette, borderColor));
|
||||
|
||||
// Cambia la textura
|
||||
texture = (options->palette == p_zxspectrum) ? textureA : textureB;
|
||||
texture = (options.palette == p_zxspectrum) ? textureA : textureB;
|
||||
|
||||
// Pone la nueva textura a los tiles animados
|
||||
for (auto tile : aTile)
|
||||
|
||||
@@ -1,21 +1,19 @@
|
||||
#pragma once
|
||||
|
||||
#include <SDL2/SDL.h>
|
||||
#include "jail_engine/asset.h"
|
||||
#include "jail_engine/debug.h"
|
||||
#include "jail_engine/jail_audio.h"
|
||||
#include "jail_engine/screen.h"
|
||||
#include "jail_engine/sprite.h"
|
||||
#include "jail_engine/utils.h"
|
||||
#include "const.h"
|
||||
#include "enemy.h"
|
||||
#include "item_tracker.h"
|
||||
#include "item.h"
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#ifndef ROOM_H
|
||||
#define ROOM_H
|
||||
#include <SDL2/SDL_rect.h> // Para SDL_Rect, SDL_Point
|
||||
#include <SDL2/SDL_render.h> // Para SDL_Renderer, SDL_Texture
|
||||
#include <string> // Para string, basic_string
|
||||
#include <vector> // Para vector
|
||||
#include "enemy.h" // Para enemy_t
|
||||
#include "item.h" // Para item_t
|
||||
#include "utils.h" // Para h_line_t, color_t, d_line_t, v_line_t
|
||||
class Asset;
|
||||
class Debug;
|
||||
class ItemTracker;
|
||||
class Screen;
|
||||
class Sprite;
|
||||
class Texture;
|
||||
struct JA_Sound_t;
|
||||
|
||||
enum tile_e
|
||||
{
|
||||
@@ -75,19 +73,18 @@ class Room
|
||||
{
|
||||
private:
|
||||
// Objetos y punteros
|
||||
Screen *screen; // Objeto encargado de dibujar en pantalla
|
||||
SDL_Renderer *renderer; // El renderizador de la ventana
|
||||
Asset *asset; // Objeto con la ruta a todos los ficheros de recursos
|
||||
Debug *debug; // Objeto para gestionar la información de debug
|
||||
std::vector<Enemy *> enemies; // Listado con los enemigos de la habitación
|
||||
std::vector<Item *> items; // Listado con los items que hay en la habitación
|
||||
Texture *texture; // Textura con los graficos de la habitación
|
||||
Texture *textureA; // Textura con los graficos de la habitación
|
||||
Texture *textureB; // Textura con los graficos de la habitación
|
||||
Asset *asset; // Objeto con la ruta a todos los ficheros de recursos
|
||||
Screen *screen; // Objeto encargado de dibujar en pantalla
|
||||
ItemTracker *itemTracker; // Lleva el control de los objetos recogidos
|
||||
SDL_Renderer *renderer; // El renderizador de la ventana
|
||||
SDL_Texture *mapTexture; // Textura para dibujar el mapa de la habitación
|
||||
int *itemsPicked; // Puntero a la cantidad de items recogidos que lleva el juego
|
||||
Debug *debug; // Objeto para gestionar la información de debug
|
||||
options_t *options; // Puntero a las opciones del juego
|
||||
|
||||
// Variables
|
||||
std::string number; // Numero de la habitación
|
||||
@@ -162,7 +159,7 @@ private:
|
||||
|
||||
public:
|
||||
// Constructor
|
||||
Room(room_t *room, SDL_Renderer *renderer, Screen *screen, Asset *asset, options_t *options, ItemTracker *itemTracker, int *itemsPicked, bool jailEnabled, Debug *debug);
|
||||
Room(room_t *room, ItemTracker *itemTracker, int *itemsPicked, bool jailEnabled);
|
||||
|
||||
// Destructor
|
||||
~Room();
|
||||
@@ -253,6 +250,4 @@ public:
|
||||
|
||||
// Obten la direccion de las superficies automaticas
|
||||
int getAutoSurfaceDirection();
|
||||
};
|
||||
|
||||
#endif
|
||||
};
|
||||
@@ -1,12 +1,7 @@
|
||||
#pragma once
|
||||
|
||||
#include <SDL2/SDL.h>
|
||||
#include "jail_engine/utils.h"
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#ifndef ROOM_TRACKER_H
|
||||
#define ROOM_TRACKER_H
|
||||
#include <string> // Para string
|
||||
#include <vector> // Para vector
|
||||
|
||||
class RoomTracker
|
||||
{
|
||||
@@ -27,5 +22,3 @@ public:
|
||||
// Añade la habitación a la lista
|
||||
bool addRoom(std::string name);
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
@@ -1,20 +1,26 @@
|
||||
#include "scoreboard.h"
|
||||
#include <fstream>
|
||||
#include <sstream>
|
||||
#include <SDL2/SDL_rect.h> // Para SDL_Rect
|
||||
#include <SDL2/SDL_timer.h> // Para SDL_GetTicks
|
||||
#include "animatedsprite.h" // Para AnimatedSprite
|
||||
#include "const.h" // Para BLOCK, PLAY_AREA_HEIGHT, PLAY_AREA_WIDTH
|
||||
#include "resource.h" // Para Resource
|
||||
#include "text.h" // Para Text
|
||||
#include "texture.h" // Para Texture
|
||||
#include "options.h"
|
||||
#include "screen.h"
|
||||
#include "asset.h"
|
||||
class Asset;
|
||||
|
||||
// Constructor
|
||||
ScoreBoard::ScoreBoard(SDL_Renderer *renderer, Resource *resource, Asset *asset, options_t *options, board_t *board)
|
||||
Scoreboard::Scoreboard(board_t *board)
|
||||
: renderer(Screen::get()->getRenderer()),
|
||||
resource(Resource::get()),
|
||||
asset(Asset::get()),
|
||||
board(board)
|
||||
{
|
||||
// Obten punteros a objetos
|
||||
this->renderer = renderer;
|
||||
this->resource = resource;
|
||||
this->asset = asset;
|
||||
this->options = options;
|
||||
this->board = board;
|
||||
|
||||
// Reserva memoria para los objetos
|
||||
itemTexture = resource->getTexture("items.png");
|
||||
const std::string playerANI = options->cheat.altSkin ? "player2.ani" : "player.ani";
|
||||
const std::string playerANI = options.cheat.altSkin ? "player2.ani" : "player.ani";
|
||||
sprite = new AnimatedSprite(renderer, resource->getAnimation(playerANI));
|
||||
sprite->setCurrentAnimation("walk_menu");
|
||||
text = new Text(resource->getOffset("smb2.txt"), resource->getTexture("smb2.png"), renderer);
|
||||
@@ -25,25 +31,25 @@ ScoreBoard::ScoreBoard(SDL_Renderer *renderer, Resource *resource, Asset *asset,
|
||||
paused = false;
|
||||
timePaused = 0;
|
||||
totalTimePaused = 0;
|
||||
itemsColor = stringToColor(options->palette, "white");
|
||||
itemsColor = stringToColor(options.palette, "white");
|
||||
|
||||
// Inicializa el vector de colores
|
||||
const std::vector<std::string> vColors = {"blue", "magenta", "green", "cyan", "yellow", "white", "bright_blue", "bright_magenta", "bright_green", "bright_cyan", "bright_yellow", "bright_white"};
|
||||
for (auto v : vColors)
|
||||
{
|
||||
color.push_back(stringToColor(options->palette, v));
|
||||
color.push_back(stringToColor(options.palette, v));
|
||||
}
|
||||
}
|
||||
|
||||
// Destructor
|
||||
ScoreBoard::~ScoreBoard()
|
||||
Scoreboard::~Scoreboard()
|
||||
{
|
||||
delete sprite;
|
||||
delete text;
|
||||
}
|
||||
|
||||
// Pinta el objeto en pantalla
|
||||
void ScoreBoard::render()
|
||||
void Scoreboard::render()
|
||||
{
|
||||
// Anclas
|
||||
const int line1 = 19 * BLOCK;
|
||||
@@ -82,15 +88,15 @@ void ScoreBoard::render()
|
||||
this->text->writeColored(BLOCK, line1, "Items collected ", board->color);
|
||||
this->text->writeColored(17 * BLOCK, line1, itemsTxt, itemsColor);
|
||||
this->text->writeColored(20 * BLOCK, line1, " Time ", board->color);
|
||||
this->text->writeColored(26 * BLOCK, line1, timeTxt, stringToColor(options->palette, "white"));
|
||||
this->text->writeColored(26 * BLOCK, line1, timeTxt, stringToColor(options.palette, "white"));
|
||||
|
||||
const std::string roomsTxt = std::to_string(board->rooms / 100) + std::to_string((board->rooms % 100) / 10) + std::to_string(board->rooms % 10);
|
||||
this->text->writeColored(22 * BLOCK, line2, "Rooms", stringToColor(options->palette, "white"));
|
||||
this->text->writeColored(28 * BLOCK, line2, roomsTxt, stringToColor(options->palette, "white"));
|
||||
this->text->writeColored(22 * BLOCK, line2, "Rooms", stringToColor(options.palette, "white"));
|
||||
this->text->writeColored(28 * BLOCK, line2, roomsTxt, stringToColor(options.palette, "white"));
|
||||
}
|
||||
|
||||
// Actualiza las variables del objeto
|
||||
void ScoreBoard::update()
|
||||
void Scoreboard::update()
|
||||
{
|
||||
counter++;
|
||||
sprite->update();
|
||||
@@ -105,7 +111,7 @@ void ScoreBoard::update()
|
||||
}
|
||||
|
||||
// Obtiene el tiempo transcurrido de partida
|
||||
ScoreBoard::clock_t ScoreBoard::getTime()
|
||||
Scoreboard::clock_t Scoreboard::getTime()
|
||||
{
|
||||
const Uint32 timeElapsed = SDL_GetTicks() - board->iniClock - totalTimePaused;
|
||||
|
||||
@@ -119,7 +125,7 @@ ScoreBoard::clock_t ScoreBoard::getTime()
|
||||
}
|
||||
|
||||
// Recarga la textura
|
||||
void ScoreBoard::reLoadTexture()
|
||||
void Scoreboard::reLoadTexture()
|
||||
{
|
||||
sprite->getTexture()->reLoad();
|
||||
// playerTexture->reLoad();
|
||||
@@ -128,33 +134,33 @@ void ScoreBoard::reLoadTexture()
|
||||
}
|
||||
|
||||
// Recarga la paleta
|
||||
void ScoreBoard::reLoadPalette()
|
||||
void Scoreboard::reLoadPalette()
|
||||
{
|
||||
// Reinicia el vector de colores
|
||||
const std::vector<std::string> vColors = {"blue", "magenta", "green", "cyan", "yellow", "white", "bright_blue", "bright_magenta", "bright_green", "bright_cyan", "bright_yellow", "bright_white"};
|
||||
color.clear();
|
||||
for (auto v : vColors)
|
||||
{
|
||||
color.push_back(stringToColor(options->palette, v));
|
||||
color.push_back(stringToColor(options.palette, v));
|
||||
}
|
||||
}
|
||||
|
||||
// Pone el marcador en modo pausa
|
||||
void ScoreBoard::pause()
|
||||
void Scoreboard::pause()
|
||||
{
|
||||
paused = true;
|
||||
timePaused = SDL_GetTicks();
|
||||
}
|
||||
|
||||
// Quita el modo pausa del marcador
|
||||
void ScoreBoard::resume()
|
||||
void Scoreboard::resume()
|
||||
{
|
||||
paused = false;
|
||||
totalTimePaused += SDL_GetTicks() - timePaused;
|
||||
}
|
||||
|
||||
// Actualiza el color de la cantidad de items recogidos
|
||||
void ScoreBoard::updateItemsColor()
|
||||
void Scoreboard::updateItemsColor()
|
||||
{
|
||||
if (!board->jailEnabled)
|
||||
{
|
||||
@@ -163,16 +169,16 @@ void ScoreBoard::updateItemsColor()
|
||||
|
||||
if (counter % 20 < 10)
|
||||
{
|
||||
itemsColor = stringToColor(options->palette, "white");
|
||||
itemsColor = stringToColor(options.palette, "white");
|
||||
}
|
||||
else
|
||||
{
|
||||
itemsColor = stringToColor(options->palette, "magenta");
|
||||
itemsColor = stringToColor(options.palette, "magenta");
|
||||
}
|
||||
}
|
||||
|
||||
// Devuelve la cantidad de minutos de juego transcurridos
|
||||
int ScoreBoard::getMinutes()
|
||||
int Scoreboard::getMinutes()
|
||||
{
|
||||
return getTime().minutes;
|
||||
}
|
||||
@@ -1,16 +1,15 @@
|
||||
#pragma once
|
||||
|
||||
#include <SDL2/SDL.h>
|
||||
#include "jail_engine/animatedsprite.h"
|
||||
#include "jail_engine/asset.h"
|
||||
#include "jail_engine/resource.h"
|
||||
#include "jail_engine/text.h"
|
||||
#include "jail_engine/utils.h"
|
||||
#include "const.h"
|
||||
#include <string>
|
||||
|
||||
#ifndef SCOREBOARD_H
|
||||
#define SCOREBOARD_H
|
||||
#include <SDL2/SDL_render.h> // Para SDL_Renderer
|
||||
#include <SDL2/SDL_stdinc.h> // Para Uint32
|
||||
#include <string> // Para basic_string, string
|
||||
#include <vector> // Para vector
|
||||
#include "utils.h" // Para color_t
|
||||
class AnimatedSprite;
|
||||
class Asset;
|
||||
class Resource;
|
||||
class Text;
|
||||
class Texture;
|
||||
|
||||
struct board_t
|
||||
{
|
||||
@@ -23,7 +22,7 @@ struct board_t
|
||||
bool jailEnabled; // Indica si se puede entrar a la Jail
|
||||
};
|
||||
|
||||
class ScoreBoard
|
||||
class Scoreboard
|
||||
{
|
||||
private:
|
||||
struct clock_t
|
||||
@@ -42,7 +41,6 @@ private:
|
||||
Text *text; // Objeto para escribir texto
|
||||
Texture *itemTexture; // Textura con los graficos para las vidas
|
||||
board_t *board; // Contiene las variables a mostrar en el marcador
|
||||
options_t *options; // Puntero a las opciones del juego
|
||||
|
||||
// Variables
|
||||
std::vector<color_t> color; // Vector con los colores del objeto
|
||||
@@ -62,10 +60,10 @@ private:
|
||||
|
||||
public:
|
||||
// Constructor
|
||||
ScoreBoard(SDL_Renderer *renderer, Resource *resource, Asset *asset, options_t *options, board_t *board);
|
||||
Scoreboard(board_t *board);
|
||||
|
||||
// Destructor
|
||||
~ScoreBoard();
|
||||
~Scoreboard();
|
||||
|
||||
// Pinta el objeto en pantalla
|
||||
void render();
|
||||
@@ -88,5 +86,3 @@ public:
|
||||
// Devuelve la cantidad de minutos de juego transcurridos
|
||||
int getMinutes();
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
492
source/screen.cpp
Normal file
492
source/screen.cpp
Normal file
@@ -0,0 +1,492 @@
|
||||
#include "screen.h"
|
||||
#include <SDL2/SDL_error.h> // Para SDL_GetError
|
||||
#include <SDL2/SDL_events.h> // Para SDL_DISABLE, SDL_ENABLE
|
||||
#include <SDL2/SDL_mouse.h> // Para SDL_ShowCursor
|
||||
#include <SDL2/SDL_pixels.h> // Para SDL_PIXELFORMAT_RGBA8888
|
||||
#include <algorithm> // Para max, min
|
||||
#include <fstream> // Para basic_ostream, operator<<, basic_ifstream
|
||||
#include <iostream> // Para cout
|
||||
#include <iterator> // Para istreambuf_iterator, operator!=
|
||||
#include <string> // Para basic_string, char_traits, string
|
||||
#include "asset.h" // Para Asset
|
||||
#include "jail_shader.h" // Para init, render
|
||||
#include "notifier.h" // Para Notify
|
||||
#include "options.h"
|
||||
|
||||
// [SINGLETON]
|
||||
Screen *Screen::screen_ = nullptr;
|
||||
|
||||
// [SINGLETON] Crearemos el objeto con esta función estática
|
||||
void Screen::init(SDL_Window *window, SDL_Renderer *renderer)
|
||||
{
|
||||
Screen::screen_ = new Screen(window, renderer);
|
||||
}
|
||||
|
||||
// [SINGLETON] Destruiremos el objeto con esta función estática
|
||||
void Screen::destroy()
|
||||
{
|
||||
delete Screen::screen_;
|
||||
}
|
||||
|
||||
// [SINGLETON] Con este método obtenemos el objeto y podemos trabajar con él
|
||||
Screen *Screen::get()
|
||||
{
|
||||
return Screen::screen_;
|
||||
}
|
||||
|
||||
// Constructor
|
||||
Screen::Screen(SDL_Window *window, SDL_Renderer *renderer)
|
||||
: window_(window),
|
||||
renderer_(renderer)
|
||||
{
|
||||
game_canvas_width_ = options.gameWidth;
|
||||
game_canvas_height_ = options.gameHeight;
|
||||
notification_logical_width_ = game_canvas_width_;
|
||||
notification_logical_height_ = game_canvas_height_;
|
||||
|
||||
iniFade();
|
||||
iniSpectrumFade();
|
||||
|
||||
// Define el color del borde para el modo de pantalla completa
|
||||
border_color_ = {0x00, 0x00, 0x00};
|
||||
|
||||
// Crea la textura donde se dibujan los graficos del juego
|
||||
game_canvas_ = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, game_canvas_width_, game_canvas_height_);
|
||||
if (game_canvas_ == nullptr)
|
||||
{
|
||||
if (options.console)
|
||||
{
|
||||
std::cout << "gameCanvas could not be created!\nSDL Error: " << SDL_GetError() << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
// Crea la textura donde se dibuja el borde que rodea el area de juego
|
||||
border_canvas_ = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, game_canvas_width_ + options.borderWidth * 2, game_canvas_height_ + options.borderHeight * 2);
|
||||
if (border_canvas_ == nullptr)
|
||||
{
|
||||
if (options.console)
|
||||
{
|
||||
std::cout << "borderCanvas could not be created!\nSDL Error: " << SDL_GetError() << std::endl;
|
||||
}
|
||||
}
|
||||
setBorderColor(border_color_);
|
||||
|
||||
// Establece el modo de video
|
||||
setVideoMode(options.videoMode);
|
||||
|
||||
// Muestra la ventana
|
||||
SDL_ShowWindow(window);
|
||||
}
|
||||
|
||||
// Destructor
|
||||
Screen::~Screen()
|
||||
{
|
||||
SDL_DestroyTexture(game_canvas_);
|
||||
SDL_DestroyTexture(border_canvas_);
|
||||
}
|
||||
|
||||
// Limpia la pantalla
|
||||
void Screen::clean(color_t color)
|
||||
{
|
||||
SDL_SetRenderDrawColor(renderer_, color.r, color.g, color.b, 0xFF);
|
||||
SDL_RenderClear(renderer_);
|
||||
}
|
||||
|
||||
// Prepara para empezar a dibujar en la textura de juego
|
||||
void Screen::start()
|
||||
{
|
||||
SDL_SetRenderTarget(renderer_, game_canvas_);
|
||||
}
|
||||
|
||||
// Prepara para empezar a dibujar en la textura del borde
|
||||
void Screen::startDrawOnBorder()
|
||||
{
|
||||
SDL_SetRenderTarget(renderer_, border_canvas_);
|
||||
}
|
||||
|
||||
// Vuelca el contenido del renderizador en pantalla
|
||||
void Screen::render()
|
||||
{
|
||||
// Renderiza sobre gameCanvas los overlays
|
||||
renderNotifications();
|
||||
|
||||
// Si está el borde activo, vuelca gameCanvas sobre borderCanvas
|
||||
if (options.borderEnabled)
|
||||
{
|
||||
gameCanvasToBorderCanvas();
|
||||
}
|
||||
|
||||
// Muestra el contenido por pantalla
|
||||
renderPresent();
|
||||
}
|
||||
|
||||
// Establece el modo de video
|
||||
void Screen::setVideoMode(int videoMode)
|
||||
{
|
||||
// Aplica el modo de video
|
||||
SDL_SetWindowFullscreen(window_, videoMode);
|
||||
|
||||
// Modo ventana
|
||||
if (videoMode == 0)
|
||||
{
|
||||
// Muestra el puntero
|
||||
SDL_ShowCursor(SDL_ENABLE);
|
||||
|
||||
// Modifica el tamaño de la ventana en función del borde
|
||||
if (options.borderEnabled)
|
||||
{
|
||||
window_width_ = game_canvas_width_ + options.borderWidth * 2;
|
||||
window_height_ = game_canvas_height_ + options.borderHeight * 2;
|
||||
dest_ = {options.borderWidth, options.borderHeight, game_canvas_width_, game_canvas_height_};
|
||||
}
|
||||
|
||||
else
|
||||
{
|
||||
window_width_ = game_canvas_width_;
|
||||
window_height_ = game_canvas_height_;
|
||||
dest_ = {0, 0, game_canvas_width_, game_canvas_height_};
|
||||
}
|
||||
|
||||
// Modifica el tamaño de la ventana
|
||||
SDL_SetWindowSize(window_, window_width_ * options.windowSize, window_height_ * options.windowSize);
|
||||
SDL_SetWindowPosition(window_, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED);
|
||||
}
|
||||
|
||||
// Si está activo el modo de pantalla completa añade el borde
|
||||
else if (videoMode == SDL_WINDOW_FULLSCREEN_DESKTOP)
|
||||
{
|
||||
// Oculta el puntero
|
||||
SDL_ShowCursor(SDL_DISABLE);
|
||||
|
||||
// Obten el alto y el ancho de la ventana
|
||||
SDL_GetWindowSize(window_, &window_width_, &window_height_);
|
||||
|
||||
// Aplica el escalado al rectangulo donde se pinta la textura del juego
|
||||
if (options.integerScale)
|
||||
{
|
||||
// Calcula el tamaño de la escala máxima
|
||||
int scale = 0;
|
||||
while (((game_canvas_width_ * (scale + 1)) <= window_width_) && ((game_canvas_height_ * (scale + 1)) <= window_height_))
|
||||
{
|
||||
scale++;
|
||||
}
|
||||
|
||||
dest_.w = game_canvas_width_ * scale;
|
||||
dest_.h = game_canvas_height_ * scale;
|
||||
dest_.x = (window_width_ - dest_.w) / 2;
|
||||
dest_.y = (window_height_ - dest_.h) / 2;
|
||||
}
|
||||
else if (options.keepAspect)
|
||||
{
|
||||
float ratio = (float)game_canvas_width_ / (float)game_canvas_height_;
|
||||
if ((window_width_ - game_canvas_width_) >= (window_height_ - game_canvas_height_))
|
||||
{
|
||||
dest_.h = window_height_;
|
||||
dest_.w = (int)((window_height_ * ratio) + 0.5f);
|
||||
dest_.x = (window_width_ - dest_.w) / 2;
|
||||
dest_.y = (window_height_ - dest_.h) / 2;
|
||||
}
|
||||
else
|
||||
{
|
||||
dest_.w = window_width_;
|
||||
dest_.h = (int)((window_width_ / ratio) + 0.5f);
|
||||
dest_.x = (window_width_ - dest_.w) / 2;
|
||||
dest_.y = (window_height_ - dest_.h) / 2;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
dest_.w = window_width_;
|
||||
dest_.h = window_height_;
|
||||
dest_.x = dest_.y = 0;
|
||||
}
|
||||
}
|
||||
|
||||
// Modifica el tamaño del renderizador
|
||||
SDL_RenderSetLogicalSize(renderer_, window_width_, window_height_);
|
||||
|
||||
// Actualiza las opciones
|
||||
options.videoMode = videoMode;
|
||||
options.screen.windowWidth = window_width_;
|
||||
options.screen.windowHeight = window_height_;
|
||||
|
||||
// Reinicia los shaders
|
||||
if (options.shaders)
|
||||
{
|
||||
const std::string glsl_file = options.screen.windowHeight == 192 ? "crtpi_192.glsl" : "crtpi_240.glsl";
|
||||
std::ifstream f(Asset::get()->get(glsl_file).c_str());
|
||||
std::string source((std::istreambuf_iterator<char>(f)), std::istreambuf_iterator<char>());
|
||||
|
||||
if (options.borderEnabled)
|
||||
{
|
||||
shader::init(window_, border_canvas_, source.c_str());
|
||||
}
|
||||
else
|
||||
{
|
||||
shader::init(window_, game_canvas_, source.c_str());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Camibia entre pantalla completa y ventana
|
||||
void Screen::toggleVideoMode()
|
||||
{
|
||||
options.videoMode = (options.videoMode == 0) ? SDL_WINDOW_FULLSCREEN_DESKTOP : 0;
|
||||
setVideoMode(options.videoMode);
|
||||
}
|
||||
|
||||
// Cambia el tamaño de la ventana
|
||||
void Screen::setWindowSize(int size)
|
||||
{
|
||||
options.windowSize = size;
|
||||
setVideoMode(0);
|
||||
}
|
||||
|
||||
// Reduce el tamaño de la ventana
|
||||
void Screen::decWindowSize()
|
||||
{
|
||||
--options.windowSize;
|
||||
options.windowSize = std::max(options.windowSize, 1);
|
||||
setVideoMode(0);
|
||||
}
|
||||
|
||||
// Aumenta el tamaño de la ventana
|
||||
void Screen::incWindowSize()
|
||||
{
|
||||
++options.windowSize;
|
||||
options.windowSize = std::min(options.windowSize, 4);
|
||||
setVideoMode(0);
|
||||
}
|
||||
|
||||
// Cambia el color del borde
|
||||
void Screen::setBorderColor(color_t color)
|
||||
{
|
||||
border_color_ = color;
|
||||
auto temp = SDL_GetRenderTarget(renderer_);
|
||||
SDL_SetRenderTarget(renderer_, border_canvas_);
|
||||
SDL_SetRenderDrawColor(renderer_, border_color_.r, border_color_.g, border_color_.b, 0xFF);
|
||||
SDL_RenderClear(renderer_);
|
||||
SDL_SetRenderTarget(renderer_, temp);
|
||||
}
|
||||
|
||||
// Cambia el tipo de mezcla
|
||||
void Screen::setBlendMode(SDL_BlendMode blendMode)
|
||||
{
|
||||
SDL_SetRenderDrawBlendMode(renderer_, blendMode);
|
||||
}
|
||||
|
||||
// Establece el tamaño del borde
|
||||
void Screen::setBorderWidth(int s)
|
||||
{
|
||||
options.borderWidth = s;
|
||||
}
|
||||
|
||||
// Establece el tamaño del borde
|
||||
void Screen::setBorderHeight(int s)
|
||||
{
|
||||
options.borderHeight = s;
|
||||
}
|
||||
|
||||
// Establece si se ha de ver el borde en el modo ventana
|
||||
void Screen::setBorderEnabled(bool value)
|
||||
{
|
||||
options.borderEnabled = value;
|
||||
}
|
||||
|
||||
// Cambia entre borde visible y no visible
|
||||
void Screen::toggleBorder()
|
||||
{
|
||||
options.borderEnabled = !options.borderEnabled;
|
||||
setVideoMode(0);
|
||||
}
|
||||
|
||||
// Activa el fade
|
||||
void Screen::setFade()
|
||||
{
|
||||
fade_ = true;
|
||||
}
|
||||
|
||||
// Comprueba si ha terminado el fade
|
||||
bool Screen::fadeEnded()
|
||||
{
|
||||
if (fade_ || fade_counter_ > 0)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
// Activa el spectrum fade
|
||||
void Screen::setspectrumFade()
|
||||
{
|
||||
spectrum_fade_ = true;
|
||||
}
|
||||
|
||||
// Comprueba si ha terminado el spectrum fade
|
||||
bool Screen::spectrumFadeEnded()
|
||||
{
|
||||
if (spectrum_fade_ || spectrum_fade_counter_ > 0)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
// Inicializa las variables para el fade
|
||||
void Screen::iniFade()
|
||||
{
|
||||
fade_ = false;
|
||||
fade_counter_ = 0;
|
||||
fade_lenght_ = 200;
|
||||
}
|
||||
|
||||
// Actualiza el fade
|
||||
void Screen::updateFade()
|
||||
{
|
||||
if (!fade_)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
fade_counter_++;
|
||||
if (fade_counter_ > fade_lenght_)
|
||||
{
|
||||
iniFade();
|
||||
}
|
||||
}
|
||||
|
||||
// Dibuja el fade
|
||||
void Screen::renderFade()
|
||||
{
|
||||
if (!fade_)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
const SDL_Rect rect = {0, 0, game_canvas_width_, game_canvas_height_};
|
||||
color_t color = {0, 0, 0};
|
||||
const float step = (float)fade_counter_ / (float)fade_lenght_;
|
||||
const int alpha = 0 + (255 - 0) * step;
|
||||
SDL_SetRenderDrawColor(renderer_, color.r, color.g, color.b, alpha);
|
||||
SDL_RenderFillRect(renderer_, &rect);
|
||||
}
|
||||
|
||||
// Inicializa las variables para el fade spectrum
|
||||
void Screen::iniSpectrumFade()
|
||||
{
|
||||
spectrum_fade_ = false;
|
||||
spectrum_fade_counter_ = 0;
|
||||
spectrum_fade_lenght_ = 50;
|
||||
|
||||
spectrum_color_.clear();
|
||||
|
||||
// Inicializa el vector de colores
|
||||
const std::vector<std::string> vColors = {"black", "blue", "red", "magenta", "green", "cyan", "yellow", "bright_white"};
|
||||
for (auto v : vColors)
|
||||
{
|
||||
spectrum_color_.push_back(stringToColor(options.palette, v));
|
||||
}
|
||||
}
|
||||
|
||||
// Actualiza el spectrum fade
|
||||
void Screen::updateSpectrumFade()
|
||||
{
|
||||
if (!spectrum_fade_)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
spectrum_fade_counter_++;
|
||||
if (spectrum_fade_counter_ > spectrum_fade_lenght_)
|
||||
{
|
||||
iniSpectrumFade();
|
||||
SDL_SetTextureColorMod(game_canvas_, 255, 255, 255);
|
||||
}
|
||||
}
|
||||
|
||||
// Dibuja el spectrum fade
|
||||
void Screen::renderSpectrumFade()
|
||||
{
|
||||
if (!spectrum_fade_)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
const float step = (float)spectrum_fade_counter_ / (float)spectrum_fade_lenght_;
|
||||
const int max = spectrum_color_.size() - 1;
|
||||
const int index = max + (0 - max) * step;
|
||||
const color_t c = spectrum_color_[index];
|
||||
SDL_SetTextureColorMod(game_canvas_, c.r, c.g, c.b);
|
||||
}
|
||||
|
||||
// Actualiza los efectos
|
||||
void Screen::updateFX()
|
||||
{
|
||||
updateFade();
|
||||
updateSpectrumFade();
|
||||
}
|
||||
|
||||
// Dibuja los efectos
|
||||
void Screen::renderFX()
|
||||
{
|
||||
renderFade();
|
||||
renderSpectrumFade();
|
||||
}
|
||||
|
||||
// Dibuja las notificaciones
|
||||
void Screen::renderNotifications()
|
||||
{
|
||||
Notifier::get()->render();
|
||||
}
|
||||
|
||||
// Copia el gameCanvas en el borderCanvas
|
||||
void Screen::gameCanvasToBorderCanvas()
|
||||
{
|
||||
auto temp = SDL_GetRenderTarget(renderer_);
|
||||
SDL_SetRenderTarget(renderer_, border_canvas_);
|
||||
SDL_SetRenderDrawColor(renderer_, border_color_.r, border_color_.g, border_color_.b, 0xFF);
|
||||
SDL_RenderClear(renderer_);
|
||||
SDL_RenderCopy(renderer_, game_canvas_, nullptr, &dest_);
|
||||
SDL_SetRenderTarget(renderer_, temp);
|
||||
}
|
||||
|
||||
// Muestra el contenido de Screen por pantalla
|
||||
void Screen::renderPresent()
|
||||
{
|
||||
SDL_SetRenderTarget(renderer_, nullptr);
|
||||
SDL_SetRenderDrawColor(renderer_, border_color_.r, border_color_.g, border_color_.b, 0xFF);
|
||||
SDL_RenderClear(renderer_);
|
||||
|
||||
if (options.shaders)
|
||||
{
|
||||
// Aplica shaders y renderiza el contenido
|
||||
shader::render();
|
||||
}
|
||||
else
|
||||
{
|
||||
if (options.borderEnabled)
|
||||
{
|
||||
SDL_RenderCopy(renderer_, border_canvas_, nullptr, nullptr);
|
||||
}
|
||||
else
|
||||
{
|
||||
SDL_RenderCopy(renderer_, game_canvas_, nullptr, &dest_);
|
||||
}
|
||||
SDL_RenderPresent(renderer_);
|
||||
}
|
||||
}
|
||||
|
||||
// Cambia el estado de los shaders
|
||||
void Screen::toggleShaders()
|
||||
{
|
||||
options.shaders = !options.shaders;
|
||||
setVideoMode(options.videoMode);
|
||||
}
|
||||
|
||||
// Actualiza la lógica de la clase
|
||||
void Screen::update()
|
||||
{
|
||||
Notifier::get()->update();
|
||||
}
|
||||
159
source/screen.h
Normal file
159
source/screen.h
Normal file
@@ -0,0 +1,159 @@
|
||||
#pragma once
|
||||
|
||||
#include <SDL2/SDL_blendmode.h> // Para SDL_BlendMode
|
||||
#include <SDL2/SDL_rect.h> // Para SDL_Rect
|
||||
#include <SDL2/SDL_render.h> // Para SDL_Renderer, SDL_Texture
|
||||
#include <SDL2/SDL_video.h> // Para SDL_Window
|
||||
#include <string> // Para basic_string, string
|
||||
#include <vector> // Para vector
|
||||
#include "utils.h" // Para color_t
|
||||
class Asset;
|
||||
class Notifier;
|
||||
|
||||
constexpr int FILTER_NEAREST = 0;
|
||||
constexpr int FILTER_LINEAR = 1;
|
||||
|
||||
class Screen
|
||||
{
|
||||
private:
|
||||
// [SINGLETON] Objeto privado
|
||||
static Screen *screen_;
|
||||
|
||||
// Objetos y punteros
|
||||
SDL_Window *window_; // Ventana de la aplicación
|
||||
SDL_Renderer *renderer_; // El renderizador de la ventana
|
||||
SDL_Texture *game_canvas_; // Textura donde se dibuja el juego
|
||||
SDL_Texture *border_canvas_; // Textura donde se dibuja el borde del juego
|
||||
|
||||
// Variables
|
||||
int window_width_; // Ancho de la pantalla o ventana
|
||||
int window_height_; // Alto de la pantalla o ventana
|
||||
int game_canvas_width_; // Resolución interna del juego. Es el ancho de la textura donde se dibuja el juego
|
||||
int game_canvas_height_; // Resolución interna del juego. Es el alto de la textura donde se dibuja el juego
|
||||
SDL_Rect dest_; // Coordenadas donde se va a dibujar la textura del juego sobre la pantalla o ventana
|
||||
color_t border_color_; // Color del borde añadido a la textura de juego para rellenar la pantalla
|
||||
int notification_logical_width_; // Ancho lógico de las notificaciones en relación al tamaño de pantalla
|
||||
int notification_logical_height_; // Alto lógico de las notificaciones en relación al tamaño de pantalla
|
||||
|
||||
// Variables - Efectos
|
||||
bool fade_; // Indica si esta activo el efecto de fade
|
||||
int fade_counter_; // Temporizador para el efecto de fade
|
||||
int fade_lenght_; // Duración del fade
|
||||
bool spectrum_fade_; // Indica si esta activo el efecto de fade spectrum
|
||||
int spectrum_fade_counter_; // Temporizador para el efecto de fade spectrum
|
||||
int spectrum_fade_lenght_; // Duración del fade spectrum
|
||||
std::vector<color_t> spectrum_color_; // Colores para el fade spectrum
|
||||
|
||||
// Inicializa las variables para el fade
|
||||
void iniFade();
|
||||
|
||||
// Actualiza el fade
|
||||
void updateFade();
|
||||
|
||||
// Dibuja el fade
|
||||
void renderFade();
|
||||
|
||||
// Inicializa las variables para el fade spectrum
|
||||
void iniSpectrumFade();
|
||||
|
||||
// Actualiza el spectrum fade
|
||||
void updateSpectrumFade();
|
||||
|
||||
// Dibuja el spectrum fade
|
||||
void renderSpectrumFade();
|
||||
|
||||
// Dibuja las notificaciones
|
||||
void renderNotifications();
|
||||
|
||||
// Copia el gameCanvas en el borderCanvas
|
||||
void gameCanvasToBorderCanvas();
|
||||
|
||||
// Muestra el contenido de Screen por pantalla
|
||||
void renderPresent();
|
||||
|
||||
// Constructor
|
||||
Screen(SDL_Window *window, SDL_Renderer *renderer);
|
||||
|
||||
// Destructor
|
||||
~Screen();
|
||||
|
||||
public:
|
||||
// [SINGLETON] Crearemos el objeto con esta función estática
|
||||
static void init(SDL_Window *window, SDL_Renderer *renderer);
|
||||
|
||||
// [SINGLETON] Destruiremos el objeto con esta función estática
|
||||
static void destroy();
|
||||
|
||||
// [SINGLETON] Con este método obtenemos el objeto y podemos trabajar con él
|
||||
static Screen *get();
|
||||
|
||||
// Limpia la pantalla
|
||||
void clean(color_t color = {0x00, 0x00, 0x00});
|
||||
|
||||
// Prepara para empezar a dibujar en la textura de juego
|
||||
void start();
|
||||
|
||||
// Prepara para empezar a dibujar en la textura del borde
|
||||
void startDrawOnBorder();
|
||||
|
||||
// Vuelca el contenido del renderizador en pantalla
|
||||
void render();
|
||||
|
||||
// Actualiza la lógica de la clase
|
||||
void update();
|
||||
|
||||
// Establece el modo de video
|
||||
void setVideoMode(int videoMode);
|
||||
|
||||
// Camibia entre pantalla completa y ventana
|
||||
void toggleVideoMode();
|
||||
|
||||
// Cambia el tamaño de la ventana
|
||||
void setWindowSize(int size);
|
||||
|
||||
// Reduce el tamaño de la ventana
|
||||
void decWindowSize();
|
||||
|
||||
// Aumenta el tamaño de la ventana
|
||||
void incWindowSize();
|
||||
|
||||
// Cambia el color del borde
|
||||
void setBorderColor(color_t color);
|
||||
|
||||
// Cambia el tipo de mezcla
|
||||
void setBlendMode(SDL_BlendMode blendMode);
|
||||
|
||||
// Establece el tamaño del borde
|
||||
void setBorderWidth(int s);
|
||||
void setBorderHeight(int s);
|
||||
|
||||
// Establece si se ha de ver el borde en el modo ventana
|
||||
void setBorderEnabled(bool value);
|
||||
|
||||
// Cambia entre borde visible y no visible
|
||||
void toggleBorder();
|
||||
|
||||
// Activa el fade
|
||||
void setFade();
|
||||
|
||||
// Comprueba si ha terminado el fade
|
||||
bool fadeEnded();
|
||||
|
||||
// Activa el spectrum fade
|
||||
void setspectrumFade();
|
||||
|
||||
// Comprueba si ha terminado el spectrum fade
|
||||
bool spectrumFadeEnded();
|
||||
|
||||
// Actualiza los efectos
|
||||
void updateFX();
|
||||
|
||||
// Dibuja los efectos
|
||||
void renderFX();
|
||||
|
||||
// Cambia el estado de los shaders
|
||||
void toggleShaders();
|
||||
|
||||
// Getters
|
||||
SDL_Renderer *getRenderer() { return renderer_; }
|
||||
};
|
||||
@@ -1,4 +1,5 @@
|
||||
#include "sprite.h"
|
||||
#include "texture.h" // Para Texture
|
||||
|
||||
// Constructor
|
||||
Sprite::Sprite(int x, int y, int w, int h, Texture *texture, SDL_Renderer *renderer)
|
||||
@@ -1,10 +1,8 @@
|
||||
#pragma once
|
||||
|
||||
#include <SDL2/SDL.h>
|
||||
#include "texture.h"
|
||||
|
||||
#ifndef SPRITE_H
|
||||
#define SPRITE_H
|
||||
#include <SDL2/SDL_rect.h> // Para SDL_Rect
|
||||
#include <SDL2/SDL_render.h> // Para SDL_Renderer
|
||||
class Texture;
|
||||
|
||||
// Clase sprite
|
||||
class Sprite
|
||||
@@ -95,6 +93,4 @@ public:
|
||||
|
||||
// Establece los valores de posición y tamaño del sprite
|
||||
void setRect(SDL_Rect rect);
|
||||
};
|
||||
|
||||
#endif
|
||||
};
|
||||
102
source/stats.cpp
102
source/stats.cpp
@@ -1,15 +1,12 @@
|
||||
#include "jail_engine/jscore.h"
|
||||
#include "stats.h"
|
||||
#include <algorithm>
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
#include <sstream>
|
||||
#include <fstream> // Para basic_ostream, basic_ifstream, basic_istream, ope...
|
||||
#include <sstream> // Para basic_stringstream
|
||||
#include "utils.h" // Para op_stats_t, options_t
|
||||
#include "options.h"
|
||||
|
||||
// Constructor
|
||||
Stats::Stats(std::string file, std::string buffer, options_t *options, Online *online)
|
||||
Stats::Stats(std::string file, std::string buffer)
|
||||
{
|
||||
this->options = options;
|
||||
this->online = online;
|
||||
bufferPath = buffer;
|
||||
filePath = file;
|
||||
bufferList.clear();
|
||||
@@ -27,7 +24,6 @@ Stats::~Stats()
|
||||
checkWorstNightmare();
|
||||
|
||||
// Guarda las estadísticas
|
||||
saveToServer();
|
||||
saveToFile(bufferPath, bufferList);
|
||||
saveToFile(filePath, list);
|
||||
|
||||
@@ -37,15 +33,11 @@ Stats::~Stats()
|
||||
}
|
||||
|
||||
// Inicializador
|
||||
// Se debe llamar a este procedimiento una vez se haya creado el diccionario numero-nombre
|
||||
void Stats::init()
|
||||
// Se debe llamar a este procedimiento una vez se haya creado el diccionario numero-nombre
|
||||
{
|
||||
loadFromFile(bufferPath, bufferList);
|
||||
loadFromFile(filePath, list);
|
||||
if (options->online.enabled)
|
||||
{
|
||||
loadFromServer();
|
||||
}
|
||||
|
||||
// Vuelca los datos del buffer en la lista de estadisticas
|
||||
updateListFromBuffer();
|
||||
@@ -54,9 +46,6 @@ void Stats::init()
|
||||
// Añade una muerte a las estadisticas
|
||||
void Stats::addDeath(std::string name)
|
||||
{
|
||||
// Normaliza el nombre
|
||||
// std::replace(name.begin(), name.end(), ' ', '_');
|
||||
|
||||
// Primero busca si ya hay una entrada con ese nombre
|
||||
const int index = findByName(name, bufferList);
|
||||
if (index != -1)
|
||||
@@ -78,9 +67,6 @@ void Stats::addDeath(std::string name)
|
||||
// Añade una visita a las estadisticas
|
||||
void Stats::addVisit(std::string name)
|
||||
{
|
||||
// Normaliza el nombre
|
||||
// std::replace(name.begin(), name.end(), ' ', '_');
|
||||
|
||||
// Primero busca si ya hay una entrada con ese nombre
|
||||
const int index = findByName(name, bufferList);
|
||||
if (index != -1)
|
||||
@@ -163,62 +149,14 @@ bool Stats::loadFromFile(std::string filePath, std::vector<stats_t> &list)
|
||||
|
||||
// El fichero no existe
|
||||
else
|
||||
{ // Crea el fichero con los valores por defecto
|
||||
{
|
||||
// Crea el fichero con los valores por defecto
|
||||
saveToFile(filePath, list);
|
||||
}
|
||||
|
||||
return success;
|
||||
}
|
||||
|
||||
// Carga las estadisticas desde un servidor
|
||||
void Stats::loadFromServer()
|
||||
{
|
||||
// Limpia los datos del servidor
|
||||
// eraseServerData();
|
||||
|
||||
list.clear();
|
||||
|
||||
std::string data;
|
||||
if (options->online.enabled)
|
||||
{
|
||||
//data = jscore::getUserData(options->online.gameID, options->online.jailerID);
|
||||
data = online->getStats();
|
||||
}
|
||||
|
||||
std::stringstream ss(data);
|
||||
std::string tmp;
|
||||
|
||||
int count = 0;
|
||||
|
||||
for (int i = 0; i < (int)data.size(); ++i)
|
||||
{
|
||||
if (data[i] == ';')
|
||||
{
|
||||
count++;
|
||||
}
|
||||
}
|
||||
|
||||
while (count > 0)
|
||||
{
|
||||
stats_t stat;
|
||||
|
||||
// Obtiene el nombre
|
||||
getline(ss, tmp, ';');
|
||||
stat.name = numberToName(tmp);
|
||||
|
||||
// Obtiene las visitas
|
||||
getline(ss, tmp, ';');
|
||||
stat.visited = std::stoi(tmp);
|
||||
|
||||
// Obtiene las muertes
|
||||
getline(ss, tmp, ';');
|
||||
stat.died = std::stoi(tmp);
|
||||
|
||||
list.push_back(stat);
|
||||
count = count - 3;
|
||||
}
|
||||
}
|
||||
|
||||
// Guarda las estadisticas en un fichero
|
||||
void Stats::saveToFile(std::string filePath, std::vector<stats_t> &list)
|
||||
{
|
||||
@@ -236,21 +174,6 @@ void Stats::saveToFile(std::string filePath, std::vector<stats_t> &list)
|
||||
file.close();
|
||||
}
|
||||
|
||||
// Guarda las estadisticas en un servidor
|
||||
void Stats::saveToServer()
|
||||
{
|
||||
std::string data = "";
|
||||
if (options->online.enabled)
|
||||
{
|
||||
for (auto item : list)
|
||||
{
|
||||
data = data + nameToNumber(item.name) + ";" + std::to_string(item.visited) + ";" + std::to_string(item.died) + ";";
|
||||
}
|
||||
//jscore::setUserData(options->online.gameID, options->online.jailerID, data);
|
||||
online->setStats(data);
|
||||
}
|
||||
}
|
||||
|
||||
// Calcula cual es la habitación con más muertes
|
||||
void Stats::checkWorstNightmare()
|
||||
{
|
||||
@@ -260,7 +183,7 @@ void Stats::checkWorstNightmare()
|
||||
if (item.died > deaths)
|
||||
{
|
||||
deaths = item.died;
|
||||
options->stats.worstNightmare = item.name;
|
||||
options.stats.worstNightmare = item.name;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -320,13 +243,6 @@ void Stats::updateListFromBuffer()
|
||||
}
|
||||
}
|
||||
|
||||
// Sube los datos al servidor
|
||||
if (options->online.enabled)
|
||||
{
|
||||
saveToServer();
|
||||
bufferList.clear();
|
||||
}
|
||||
|
||||
saveToFile(bufferPath, bufferList);
|
||||
saveToFile(filePath, list);
|
||||
}
|
||||
@@ -1,12 +1,8 @@
|
||||
#pragma once
|
||||
#include <SDL2/SDL.h>
|
||||
#include "jail_engine/utils.h"
|
||||
#include "online.h"
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#ifndef STATS_H
|
||||
#define STATS_H
|
||||
#include <string> // Para string, basic_string
|
||||
#include <vector> // Para vector
|
||||
struct options_t;
|
||||
|
||||
class Stats
|
||||
{
|
||||
@@ -24,10 +20,6 @@ private:
|
||||
std::string name; // Nombre de la habitación
|
||||
};
|
||||
|
||||
// Punteros y objetos
|
||||
options_t *options; // Puntero a la variable con todas las opciones del programa
|
||||
Online *online; // Objeto para gestionar la lectura y escritura de datos en el servidor remoto
|
||||
|
||||
// Variables
|
||||
std::vector<stats_dictionary_t> dictionary; // Lista con la equivalencia nombre-numero de habitacion
|
||||
std::vector<stats_t> bufferList; // Lista con las estadisticas temporales por habitación
|
||||
@@ -41,15 +33,9 @@ private:
|
||||
// Carga las estadisticas desde un fichero
|
||||
bool loadFromFile(std::string filePath, std::vector<stats_t> &list);
|
||||
|
||||
// Carga las estadisticas desde un servidor
|
||||
void loadFromServer();
|
||||
|
||||
// Guarda las estadisticas en un fichero
|
||||
void saveToFile(std::string filePath, std::vector<stats_t> &list);
|
||||
|
||||
// Guarda las estadisticas en un servidor
|
||||
void saveToServer();
|
||||
|
||||
// Calcula cual es la habitación con más muertes
|
||||
void checkWorstNightmare();
|
||||
|
||||
@@ -64,7 +50,7 @@ private:
|
||||
|
||||
public:
|
||||
// Constructor
|
||||
Stats(std::string file, std::string buffer, options_t *options, Online *online);
|
||||
Stats(std::string file, std::string buffer);
|
||||
|
||||
// Destructor
|
||||
~Stats();
|
||||
@@ -81,6 +67,4 @@ public:
|
||||
|
||||
// Añade una entrada al diccionario
|
||||
void addDictionary(std::string number, std::string name);
|
||||
};
|
||||
|
||||
#endif
|
||||
};
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user