Skip to content

Commit

Permalink
Convert game_ai from CPP to C. Final Step
Browse files Browse the repository at this point in the history
  • Loading branch information
MatPoliquin committed Jan 21, 2025
1 parent 5d59960 commit 659c0b0
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 39 deletions.
60 changes: 28 additions & 32 deletions ai/game_ai.cpp → ai/game_ai.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include "game_ai.h"
#include <stdio.h>
#include <retro_assert.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>

Expand All @@ -10,28 +10,30 @@
#include <dlfcn.h>
#endif

#include <retro_assert.h>

#include "../deps/game_ai_lib/GameAI.h"

#define GAME_AI_MAX_PLAYERS 2

void * ga = nullptr;
volatile void * g_ram_ptr = nullptr;
void * ga = NULL;
volatile void * g_ram_ptr = NULL;
volatile int g_ram_size = 0;
volatile signed short int g_buttons_bits[GAME_AI_MAX_PLAYERS] = {0};
volatile int g_frameCount = 0;
volatile char game_ai_lib_path[1024] = {0};
volatile char g_game_name[1024] = {0};
retro_log_printf_t g_log = nullptr;
retro_log_printf_t g_log = NULL;

/* GameAI Lib API*/
create_game_ai_t create_game_ai = nullptr;
game_ai_lib_init_t game_ai_lib_init = nullptr;
game_ai_lib_think_t game_ai_lib_think = nullptr;
game_ai_lib_set_show_debug_t game_ai_lib_set_show_debug = nullptr;
game_ai_lib_set_debug_log_t game_ai_lib_set_debug_log = nullptr;
create_game_ai_t create_game_ai = NULL;
game_ai_lib_init_t game_ai_lib_init = NULL;
game_ai_lib_think_t game_ai_lib_think = NULL;
game_ai_lib_set_show_debug_t game_ai_lib_set_show_debug = NULL;
game_ai_lib_set_debug_log_t game_ai_lib_set_debug_log = NULL;

/* Helper functions */
extern "C" void game_ai_debug_log(int level, const char *fmt, ...)
void game_ai_debug_log(int level, const char *fmt, ...)
{
va_list vp;
va_start(vp, fmt);
Expand All @@ -44,19 +46,19 @@ extern "C" void game_ai_debug_log(int level, const char *fmt, ...)
va_end(vp);
}

void array_to_bits_16(volatile signed short & result, const bool b[16])
void array_to_bits_16(volatile signed short * result, const bool b[16])
{
for (int bit = 0; bit <= 15; bit++)
{
result |= b[bit] ? (1 << bit) : 0;
*result |= b[bit] ? (1 << bit) : 0;
}
}

/* Interface to RA */

extern "C" signed short int game_ai_input(unsigned int port, unsigned int device, unsigned int idx, unsigned int id, signed short int result)
extern signed short int game_ai_input(unsigned int port, unsigned int device, unsigned int idx, unsigned int id, signed short int result)
{
if (ga == nullptr)
if (ga == NULL)
return 0;

if (port < GAME_AI_MAX_PLAYERS)
Expand All @@ -65,11 +67,9 @@ extern "C" signed short int game_ai_input(unsigned int port, unsigned int device
return 0;
}

extern "C" void game_ai_init()
extern void game_ai_init()
{
printf("GameAIManager::Init()\n");

if (create_game_ai == nullptr)
if (create_game_ai == NULL)
{
#ifdef _WIN32
HINSTANCE hinstLib;
Expand Down Expand Up @@ -106,29 +106,27 @@ extern "C" void game_ai_init()
{
dlinfo(myso, RTLD_DI_ORIGIN, (void *) &game_ai_lib_path);

create_game_ai = reinterpret_cast<create_game_ai_t>(dlsym(myso, "create_game_ai"));
create_game_ai = (create_game_ai_t)(dlsym(myso, "create_game_ai"));
retro_assert(create_game_ai);

game_ai_lib_init = reinterpret_cast<game_ai_lib_init_t>(dlsym(myso, "game_ai_lib_init"));
game_ai_lib_init = (game_ai_lib_init_t)(dlsym(myso, "game_ai_lib_init"));
retro_assert(game_ai_lib_init);

game_ai_lib_think = reinterpret_cast<game_ai_lib_think_t>(dlsym(myso, "game_ai_lib_think"));
game_ai_lib_think = (game_ai_lib_think_t)(dlsym(myso, "game_ai_lib_think"));
retro_assert(game_ai_lib_think);

game_ai_lib_set_show_debug = reinterpret_cast<game_ai_lib_set_show_debug_t>(dlsym(myso, "game_ai_lib_set_show_debug"));
game_ai_lib_set_show_debug = (game_ai_lib_set_show_debug_t)(dlsym(myso, "game_ai_lib_set_show_debug"));
retro_assert(game_ai_lib_set_show_debug);

game_ai_lib_set_debug_log = reinterpret_cast<game_ai_lib_set_debug_log_t>(dlsym(myso, "game_ai_lib_set_debug_log"));
game_ai_lib_set_debug_log = (game_ai_lib_set_debug_log_t)(dlsym(myso, "game_ai_lib_set_debug_log"));
retro_assert(game_ai_lib_set_debug_log);
}
#endif
}
}

extern "C" void game_ai_load(const char * name, void * ram_ptr, int ram_size, retro_log_printf_t log)
extern void game_ai_load(const char * name, void * ram_ptr, int ram_size, retro_log_printf_t log)
{
printf("GameAIManager::Load\n");

strcpy((char *) &g_game_name[0], name);

g_ram_ptr = ram_ptr;
Expand All @@ -137,12 +135,12 @@ extern "C" void game_ai_load(const char * name, void * ram_ptr, int ram_size, re
g_log = log;
}

extern "C" void game_ai_think(bool override_p1, bool override_p2, bool show_debug, const void *frame_data, unsigned int frame_width, unsigned int frame_height, unsigned int frame_pitch, unsigned int pixel_format)
extern void game_ai_think(bool override_p1, bool override_p2, bool show_debug, const void *frame_data, unsigned int frame_width, unsigned int frame_height, unsigned int frame_pitch, unsigned int pixel_format)
{
if (ga)
game_ai_lib_set_show_debug(ga, show_debug);

if (ga == nullptr && g_ram_ptr != nullptr)
if (ga == NULL && g_ram_ptr != NULL)
{
ga = create_game_ai((char *) &g_game_name[0]);
retro_assert(ga);
Expand All @@ -154,9 +152,7 @@ extern "C" void game_ai_think(bool override_p1, bool override_p2, bool show_debu
strcat(&data_path[0], "/data/");
strcat(&data_path[0], (char *)g_game_name);


game_ai_lib_init(ga, (void *) g_ram_ptr, g_ram_size);

game_ai_lib_set_debug_log(ga, game_ai_debug_log);
}
}
Expand All @@ -173,13 +169,13 @@ extern "C" void game_ai_think(bool override_p1, bool override_p2, bool show_debu
if (override_p1)
{
game_ai_lib_think(ga, b, 0, frame_data, frame_width, frame_height, frame_pitch, pixel_format);
array_to_bits_16(g_buttons_bits[0], b);
array_to_bits_16(&g_buttons_bits[0], b);
}

if (override_p2)
{
game_ai_lib_think(ga, b, 1, frame_data, frame_width, frame_height, frame_pitch, pixel_format);
array_to_bits_16(g_buttons_bits[1], b);
array_to_bits_16(&g_buttons_bits[1], b);
}
}
g_frameCount=0;
Expand Down
6 changes: 6 additions & 0 deletions deps/game_ai_lib/GameAI.h
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
#pragma once

#ifdef __cplusplus

#include <bitset>
#include <string>
#include <filesystem>
#include <vector>
#include <queue>

#endif


typedef void (*debug_log_t)(int level, const char *fmt, ...);

#define GAMEAI_MAX_BUTTONS 16
#define GAMEAI_SKIPFRAMES 4


#ifdef __cplusplus

class GameAI {
Expand Down
7 changes: 7 additions & 0 deletions griffin/griffin.c
Original file line number Diff line number Diff line change
Expand Up @@ -1682,3 +1682,10 @@ CLOUD SYNC
#include "../network/cloud_sync_driver.c"
#include "../network/cloud_sync/webdav.c"
#endif

/*============================================================
GAME AI
============================================================ */
#if defined(HAVE_GAME_AI)
#include "../ai/game_ai.c"
#endif
7 changes: 0 additions & 7 deletions griffin/griffin_cpp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,10 +121,3 @@ FONTS
#endif
#endif


/*============================================================
GAME AI
============================================================ */
#if defined(HAVE_GAME_AI)
#include "../ai/game_ai.cpp"
#endif

0 comments on commit 659c0b0

Please sign in to comment.