Collab_RPG/src/main.c

202 lines
5 KiB
C
Raw Normal View History

#include "config.h"
#include "events.h"
#include "npc.h"
#include <fxlibc/printf.h>
2024-07-29 11:36:11 +02:00
#include <gint/cpu.h>
#include <gint/display.h>
#include <gint/keyboard.h>
#include <gint/timer.h>
#if USB_FEATURE
2024-07-29 11:36:11 +02:00
#include <gint/usb-ff-bulk.h>
#include <gint/usb.h>
#endif // USB_FEATURE
#if GRAYMODEOK
2024-07-29 11:36:11 +02:00
#include <gint/gray.h>
#endif // GRAYMODEOK
#include "dialogs.h"
#include "game.h"
#include "mapdata.h"
#include <stdbool.h>
#include <stdint.h>
extern bopti_image_t player_face_img;
extern Map *worldRPG[];
/* Game data (defined in "game.h")*/
2024-07-29 11:36:11 +02:00
Game game = {NULL,
{12 * PXSIZE, 36 * PXSIZE, 0, 0, 12 * PXSIZE, 36 * PXSIZE, 100,
SPEED, false, 0, false, false, true},
{{}, {}, 0},
false,
false,
false,
0
/* debug variables*/
,
false,
false,
false,
100};
2024-07-24 21:43:21 +02:00
/* screen capture management code. TODO: Clean this up! */
#if USB_FEATURE
2024-07-29 11:36:11 +02:00
void USB_feature(void) {
if(game.screenshot && usb_is_open()) {
2024-07-29 11:36:11 +02:00
#if GRAYMODEOK // This is a trick, if GRAYMODEOK is defined then
// we make the code accessible
2024-07-29 11:36:11 +02:00
if(dgray_enabled())
usb_fxlink_screenshot_gray(false);
else
2024-07-29 11:36:11 +02:00
#endif
// else we just let the usual screeshot function
2024-07-29 11:36:11 +02:00
usb_fxlink_screenshot(false);
game.screenshot = false;
}
2024-07-29 11:36:11 +02:00
if(game.record && usb_is_open()) {
2024-07-29 11:36:11 +02:00
#if GRAYMODEOK
2024-07-29 11:36:11 +02:00
if(dgray_enabled())
usb_fxlink_videocapture_gray(false);
else
2024-07-29 11:36:11 +02:00
#endif
usb_fxlink_videocapture(false);
}
2024-07-29 11:36:11 +02:00
}
#endif
/* Timer callback */
int update_time(void) {
game.frame_duration++;
return TIMER_CONTINUE;
}
2023-07-07 14:50:30 +02:00
int main(void) {
2024-07-29 11:36:11 +02:00
__printf_enable_fp();
2024-07-29 11:36:11 +02:00
int timer;
timer = timer_configure(TIMER_TMU, 1000, GINT_CALL(update_time));
2024-07-29 11:36:11 +02:00
if(timer < 0) {
return -1;
}
timer_start(timer);
2023-08-10 18:33:17 +02:00
game.map_level = worldRPG[0];
2024-07-20 16:53:13 +02:00
events_init_handler(&game.handler);
2024-07-29 11:36:11 +02:00
events_bind_variable(&game.handler, (int *)&game.player.life, "life");
2024-07-20 16:53:13 +02:00
events_bind_variable(&game.handler, &game.mana, "mana");
reload_npc(&game);
2024-07-29 11:36:11 +02:00
#if USB_FEATURE
usb_interface_t const *interfaces[] = {&usb_ff_bulk, NULL};
usb_open(interfaces, GINT_CALL_NULL);
#endif
/* start grayscale engine */
2024-07-29 11:36:11 +02:00
#if GRAYMODEOK
dgray(DGRAY_ON);
#endif
2024-07-29 11:36:11 +02:00
do {
2024-07-30 17:49:40 +02:00
/*clears & renders everything */
2024-07-25 13:07:19 +02:00
game_draw(&game);
2024-07-30 17:49:40 +02:00
dprint(0,30,0,"draw time : %d", game.frame_duration);
2024-07-29 11:36:11 +02:00
#if DEBUGMODE && GINT_RENDER_RGB
if(game.debug_map) {
dfont(NULL);
drect(5, 5, 390, 55, C_WHITE);
dprint(10, 10, C_RED, "Map[%d] : Xmn %d Ymn %d Xmx %d Ymx %d", 0,
worldRPG[0]->xmin, worldRPG[0]->ymin, worldRPG[0]->xmax,
worldRPG[0]->ymax);
dprint(10, 20, C_RED, "Map[%d] : Xmn %d Ymn %d Xmx %d Ymx %d", 1,
worldRPG[1]->xmin, worldRPG[1]->ymin, worldRPG[1]->xmax,
worldRPG[1]->ymax);
dprint(10, 30, C_RED, "Map[%d] : Xmn %d Ymn %d Xmx %d Ymx %d", 2,
worldRPG[2]->xmin, worldRPG[2]->ymin, worldRPG[2]->xmax,
worldRPG[2]->ymax);
dprint(10, 40, C_RED, "Map[%d] : Xmn %d Ymn %d Xmx %d Ymx %d", 3,
worldRPG[3]->xmin, worldRPG[3]->ymin, worldRPG[3]->xmax,
worldRPG[3]->ymax);
}
if(game.debug_player) {
dfont(NULL);
drect(5, 55, 390, 75, C_WHITE);
dprint(10, 60, C_BLUE, "X= %d - Y= %d / Wx= %d - Wy= %d",
game.player.x, game.player.y, game.player.wx,
game.player.wy);
}
if(game.debug_extra) {
dfont(NULL);
for(int i = 0; i < game.map_level->nbextradata; i++)
dprint(10, 90 + i * 15, C_RED,
"X= %d - Y= %d - T: %d - ID: %d - S: %c",
game.map_level->extradata[i].x,
game.map_level->extradata[i].y,
game.map_level->extradata[i].dialogID,
game.map_level
->dialogs[game.map_level->extradata[i].dialogID]
.ID,
game.map_level
->dialogs[game.map_level->extradata[i].dialogID]
.conclusion1[0]);
}
#endif
/* start the logic of the game */
game_logic(&game);
/* Screen blit */
dupdate();
2024-07-29 11:36:11 +02:00
/* Screen capture feature if enabled */
#if USB_FEATURE
USB_feature();
#endif
/* Management of the inputs */
2024-07-25 13:07:19 +02:00
game_get_inputs(&game);
/* Run the game at max. 50fps */
2024-07-29 11:36:11 +02:00
while(game.frame_duration < 20)
sleep();
/* Reset frame_duration for the next frame */
game.frame_duration = 0;
2024-07-29 11:36:11 +02:00
} while(!game.exittoOS); // want to exit ?
2023-07-06 21:16:07 +02:00
2024-07-29 11:36:11 +02:00
/* shutdown grayengine*/
#if GRAYMODEOK
dgray(DGRAY_OFF);
#endif
2024-07-29 11:36:11 +02:00
/* close USB */
#if USB_FEATURE
usb_close();
#endif
timer_stop(timer);
return 1;
}