spacebox/src/Game.hpp

126 lines
3.2 KiB
C++

#ifndef Game_h_
#define Game_h_
#include <stdlib.h>
#include <vector>
#include <string>
#include <iostream>
#include <sstream>
#include <ctime>
#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>
#else
#include "glew/glew.h"
#endif
#include "Node.hpp"
#include "Input.hpp"
#include "Display.hpp"
#include "Configuration.hpp"
#include "Delegate.hpp"
#include "Recorder.hpp"
#include "extension.hpp"
#include "Sprite.hpp"
#include "Audio.hpp"
struct FramerateIndicator : Sprite
{
FramerateIndicator(Node*);
void respond(SDL_Event&);
SDL_Surface* get_surface();
void refresh();
};
class Game : public Node
{
private:
static void sdl_log_override(void*, int, SDL_LogPriority, const char*);
public:
Game(const Game&) = delete;
Game& operator=(const Game&) = delete;
Game(Game&&) = delete;
Game& operator=(Game&&) = delete;
static const int DEFAULT_SDL_LOG_CATEGORY = SDL_LOG_CATEGORY_CUSTOM;
SDL_Window* window;
SDL_Renderer* renderer = NULL;
SDL_GLContext glcontext = NULL;
int frame_count_this_second = 0, framerate, ticks, last_frame_length;
float frame_length = 1000.0 / 60.0, frame_time_overflow = 0, last_frame_timestamp,
last_frame_count_timestamp, emscripten_previous_time;
bool done = false, show_framerate = true, is_gl_context = true;
Configuration configuration = Configuration(this);
Delegate delegate = Delegate(this);
Display display = Display(this);
Recorder recorder = Recorder(this);
Input input = Input(this);
Audio audio = Audio(this);
std::vector<float> frame_length_history;
TTF_Font* bp_mono_font = NULL;
FramerateIndicator framerate_indicator = FramerateIndicator(this);
Game();
~Game();
virtual void reset() { activate(); }
void print_error(const std::string&);
void print_sdl_error(const std::string&);
void print_gl_attributes();
void print_frame_length_history();
void load_sdl_context();
void load_gl_context();
void log(const std::string&, const int = DEFAULT_SDL_LOG_CATEGORY) const;
void debug(const std::string&, const int = DEFAULT_SDL_LOG_CATEGORY) const;
void log_renderer_info(SDL_RendererInfo&);
bool log_gl_errors(std::string);
void log_display_mode();
void log_surface_format(SDL_Surface*, std::string = "surface");
std::string get_pixel_format_string(Uint32);
const SDL_Window* get_window() const;
SDL_Window* get_window();
const SDL_Renderer* get_renderer() const;
SDL_Renderer* get_renderer();
const Input& get_input() const;
Input& get_input();
Audio& get_audio();
glm::vec2 weight(glm::vec2);
void run();
void frame(float);
void flag_to_end();
virtual void update() {};
void set_framerate(int);
void handle_quit_event(SDL_Event&);
void quit();
virtual std::string get_class_name() const { return "Game"; }
template<typename T>
float weight(T amount)
{
return (last_frame_length / (1000.0 / 60)) * amount;
}
};
#if defined(__EMSCRIPTEN__)
void loop(void*);
#endif
#endif