Просмотр исходного кода

Do not use unnecessary class burden to simply call three callbacks

Daniele Bartolini 12 лет назад
Родитель
Сommit
8df8cfe65b
4 измененных файлов с 51 добавлено и 30 удалено
  1. 17 13
      game/Game.h
  2. 7 0
      src/Config.h.in
  3. 27 16
      src/Device.cpp
  4. 0 1
      src/Device.h

+ 17 - 13
game/Game.h

@@ -28,20 +28,24 @@ OTHER DEALINGS IN THE SOFTWARE.
 namespace crown
 {
 
-class Game
+extern "C"
 {
-public:
-
-					Game() {}
-	virtual			~Game() {}
-
-	virtual void	init() = 0;
-	virtual void	shutdown() = 0;
-	virtual void	update(float dt) = 0;
-};
-
-typedef Game* create_game_t();
-typedef void destroy_game_t(Game* game);
+	/// Called exactly once after the engine is fully initialized
+	/// and ready to use. This function is the right place to allocate
+	/// and initialize all the main components of the game.
+	void init();
+
+	/// Called just before the engine starts to deallocate resources and
+	/// subsystems leading to terminating the execution.
+	/// Here you can safely perform all the necessary deallocation/destruction
+	/// of the previously allocated game resources and/or systems. 
+	void shutdown();
+
+	/// Called once per frame, here is the place you tipically perform input checking,
+	/// updates, drawing and so on. The @dt parameter contains the last frame delta time
+	/// in seconds. 
+	void frame(float dt);
+}
 
 } // namespace crown
 

+ 7 - 0
src/Config.h.in

@@ -33,3 +33,10 @@ OTHER DEALINGS IN THE SOFTWARE.
 #cmakedefine CROWN_BUILD_OPENGL
 #cmakedefine CROWN_BUILD_OPENGLES
 #cmakedefine CROWN_USE_FLOAT
+
+// OS peculiarities
+#if defined(LINUX) || defined(ANDROID)
+	#define GAME_LIBRARY_NAME "libgame.so"
+#elif defined(WINDOWS)
+	#define GAME_LIBRARY_NAME "game.dll"
+#endif

+ 27 - 16
src/Device.cpp

@@ -52,7 +52,9 @@ OTHER DEALINGS IN THE SOFTWARE.
 namespace crown
 {
 
-static const char* GAME_LIBRARY_NAME = "libgame.so";
+static void (*game_init)(void) = NULL;
+static void (*game_shutdown)(void) = NULL;
+static void (*game_frame)(float) = NULL;
 
 //-----------------------------------------------------------------------------
 Device::Device() :
@@ -72,14 +74,13 @@ Device::Device() :
 	m_last_delta_time(0.0f),
 
 	m_filesystem(NULL),
-	m_resource_manager(NULL),
 	m_input_manager(NULL),
 	m_renderer(NULL),
 	m_debug_renderer(NULL),
 
+	m_resource_manager(NULL),
 	m_resource_archive(NULL),
 
-	m_game(NULL),
 	m_game_library(NULL)
 {
 	string::strcpy(m_preferred_root_path, string::EMPTY);
@@ -122,7 +123,16 @@ bool Device::init(int argc, char** argv)
 
 	Log::i("Initializing Game...");
 
-	const char* game_library_path = m_filesystem->build_os_path(m_filesystem->root_path(), GAME_LIBRARY_NAME);
+	// Try to locate the game library
+	if (!m_filesystem->exists(GAME_LIBRARY_NAME))
+	{
+		Log::e("Unable to find the game library in the root path.", GAME_LIBRARY_NAME);
+		return false;
+	}
+
+	// Try to load the game library and bind functions
+	const char* game_library_path = m_filesystem->os_path(GAME_LIBRARY_NAME);
+
 	m_game_library = os::open_library(game_library_path);
 
 	if (m_game_library == NULL)
@@ -131,11 +141,12 @@ bool Device::init(int argc, char** argv)
 		return false;
 	}
 
-	create_game_t* create_game = (create_game_t*)os::lookup_symbol(m_game_library, "create_game");
-
-	m_game = create_game();
+	*(void**)(&game_init) = os::lookup_symbol(m_game_library, "init");
+	*(void**)(&game_shutdown) = os::lookup_symbol(m_game_library, "shutdown");
+	*(void**)(&game_frame) = os::lookup_symbol(m_game_library, "frame");
 
-	m_game->init();
+	// Initialize the game
+	game_init();
 
 	m_is_init = true;
 
@@ -153,14 +164,14 @@ void Device::shutdown()
 		return;
 	}
 
-	m_game->shutdown();
-
-	destroy_game_t* destroy_game = (destroy_game_t*)os::lookup_symbol(m_game_library, "destroy_game");
+	// Shutdowns the game
+	game_shutdown();
 
-	destroy_game(m_game);
-	m_game = NULL;
-
-	os::close_library(m_game_library);
+	// Unload the game library
+	if (m_game_library)
+	{
+		os::close_library(m_game_library);
+	}
 
 	if (m_input_manager)
 	{
@@ -295,7 +306,7 @@ void Device::frame()
 
 	m_renderer->begin_frame();
 
-	m_game->update(last_delta_time());
+	game_frame(last_delta_time());
 
 	m_debug_renderer->draw_all();
 

+ 0 - 1
src/Device.h

@@ -141,7 +141,6 @@ private:
 	MallocAllocator			m_resource_allocator;
 
 	// The game currently running
-	Game*					m_game;
 	void*					m_game_library;
 
 private: