187 lines
5.6 KiB
C++
187 lines
5.6 KiB
C++
/* +------------------------------------------------------+
|
|
____/ \____ /| - Open source game framework licensed to freely use, |
|
|
\ / / | copy, modify and sell without restriction |
|
|
+--\ ^__^ /--+ | |
|
|
| ~/ \~ | | - created for <https://foam.shampoo.ooo> |
|
|
| ~~~~~~~~~~~~ | +------------------------------------------------------+
|
|
| SPACE ~~~~~ | /
|
|
| ~~~~~~~ BOX |/
|
|
+-------------*/
|
|
|
|
#pragma once
|
|
|
|
#include <stdlib.h>
|
|
#include <vector>
|
|
#include <string>
|
|
#include <iostream>
|
|
#include <sstream>
|
|
#include <ctime>
|
|
#include <fstream>
|
|
|
|
#define SDL_MAIN_HANDLED
|
|
#include "SDL.h"
|
|
#include "SDL_mixer.h"
|
|
#include "SDL_ttf.h"
|
|
|
|
#define GL_GLEXT_PROTOTYPES
|
|
#define GLEW_STATIC
|
|
#if defined(__EMSCRIPTEN__)
|
|
#include <emscripten.h>
|
|
#include <emscripten/html5.h>
|
|
#include <GL/glew.h>
|
|
#elif defined(__ANDROID__) || defined(ANDROID)
|
|
#include <GLES3/gl3.h>
|
|
#include <GLES3/gl3ext.h>
|
|
#else
|
|
#include "glew/glew.h"
|
|
#endif
|
|
|
|
#if defined(__ANDROID__) || defined(ANDROID)
|
|
#include <android/log.h>
|
|
#endif
|
|
|
|
#include "Node.hpp"
|
|
#include "Input.hpp"
|
|
#include "Display.hpp"
|
|
#include "Configuration.hpp"
|
|
#include "Delegate.hpp"
|
|
#include "Recorder.hpp"
|
|
#include "Audio.hpp"
|
|
#include "Log.hpp"
|
|
#include "filesystem.hpp"
|
|
#include "extension.hpp"
|
|
|
|
class Game : public Node
|
|
{
|
|
|
|
private:
|
|
|
|
int ticks;
|
|
float frame_length = 1000.0 / 60.0;
|
|
SDL_Window* _window;
|
|
|
|
inline static std::shared_ptr<TTF_Font> bp_mono_font;
|
|
|
|
inline static const std::string USER_CONFIG_PATH = "config.json";
|
|
inline static const std::string ANDROID_CONFIG_PATH = "config_android.json";
|
|
inline static const std::string WASM_CONFIG_PATH = "config_wasm.json";
|
|
|
|
/*!
|
|
* Overrides SDL's default log function to log a message to stdout/stderr and, if log is enabled in the
|
|
* global configuration, to a file. Debug level statements may be suppressed, printed to stdout, or printed to
|
|
* both stdout and file, depending on the global configuration. This shouldn't be called directly. Use
|
|
* `sb::Log::log` instead.
|
|
*
|
|
* @see sb::Log::log(const std::string&)
|
|
*
|
|
* @param userdata must be a pointer to Game
|
|
* @param category SDL log category. It is not used by SPACEBOX, so it should be sb::Log::DEFAULT_CATEGORY
|
|
* @param priority SDL log priority, which is equivalent to the values in sb::Log::Level
|
|
* @param message message as a C-style string
|
|
*/
|
|
static void sdl_log_override(void* userdata, int category, SDL_LogPriority priority, const char* message);
|
|
|
|
protected:
|
|
|
|
Configuration _configuration {this};
|
|
sb::Delegate _delegate {this};
|
|
|
|
public:
|
|
|
|
/* two-state enum equivalent to a boolean that can improve readability depending on the context */
|
|
enum class Flip {
|
|
OFF,
|
|
ON
|
|
};
|
|
|
|
/* Prevent an instance of this class from being copied or moved */
|
|
Game(const Game&) = delete;
|
|
Game& operator=(const Game&) = delete;
|
|
Game(Game&&) = delete;
|
|
Game& operator=(Game&&) = delete;
|
|
|
|
SDL_Renderer* renderer = nullptr;
|
|
SDL_GLContext glcontext = nullptr;
|
|
int frame_count_this_second = 0, last_frame_length, current_frames_per_second = 0;
|
|
float frame_time_overflow = 0, last_frame_timestamp, last_frame_count_timestamp;
|
|
bool done = false, show_framerate = true, is_gl_context = true;
|
|
sb::Display display {this};
|
|
Recorder recorder {this};
|
|
Input input {this};
|
|
Audio audio {this};
|
|
std::vector<float> frame_length_history;
|
|
|
|
Game();
|
|
virtual void reset() { activate(); }
|
|
void print_frame_length_history();
|
|
void load_sdl_context();
|
|
void load_gl_context();
|
|
GLuint load_shader(const fs::path&, GLenum) const;
|
|
bool link_shader(GLuint program) const;
|
|
void log_renderer_info(SDL_RendererInfo&);
|
|
|
|
/*!
|
|
* Write resolution, monitor refresh rate, and pixel format to the log. Taken from SDL_GetCurrentDisplayMode.html
|
|
* on the SDL wiki.
|
|
*/
|
|
void log_display_mode() const;
|
|
|
|
/*!
|
|
* Log properties of the GL context. Taken from `sdl_source/tests/testgles2.c`
|
|
*/
|
|
void log_gl_properties() const;
|
|
|
|
void log_surface_format(SDL_Surface*, std::string = "surface");
|
|
const Configuration& configuration() const;
|
|
Configuration& configuration();
|
|
const sb::Delegate& delegate() const;
|
|
sb::Delegate& delegate();
|
|
const SDL_Window* window() const;
|
|
SDL_Window* window();
|
|
const SDL_Renderer* get_renderer() const;
|
|
SDL_Renderer* get_renderer();
|
|
const Input& get_input() const;
|
|
Input& get_input();
|
|
Audio& get_audio();
|
|
static const std::shared_ptr<TTF_Font>& font();
|
|
void run();
|
|
void frame(float);
|
|
void flag_to_end();
|
|
virtual void update(float timestamp) {};
|
|
void set_framerate(int);
|
|
float get_frame_length() const;
|
|
void handle_quit_event(SDL_Event&);
|
|
void quit();
|
|
virtual std::string class_name() const { return "Game"; }
|
|
~Game();
|
|
|
|
/*!
|
|
* Applies delta timing to a value: returns the value as weighted by the amount of time passed since the
|
|
* last frame update, allowing for values to change the same amount over time independent of the frame rate.
|
|
* The amount is how much the value should change per second.
|
|
*
|
|
* @param amount any scalar value to be weighted
|
|
* @return weighted value
|
|
*/
|
|
template<typename T>
|
|
T weight(T amount) const
|
|
{
|
|
return (last_frame_length / 1000.0f) * amount;
|
|
}
|
|
|
|
};
|
|
|
|
/* Add Game class to the sb namespace. This should be the default location, but Game is left in the global namespace
|
|
* for backward compatibility.
|
|
*/
|
|
namespace sb
|
|
{
|
|
using ::Game;
|
|
}
|
|
|
|
#if defined(__EMSCRIPTEN__)
|
|
|
|
void loop(void*);
|
|
|
|
#endif
|