spacebox/src/Game.hpp

126 lines
3.2 KiB
C++
Raw Normal View History

2019-04-23 01:42:19 -04:00
#ifndef Game_h_
#define Game_h_
2021-06-26 20:25:03 -04:00
#include <stdlib.h>
#include <vector>
2019-04-23 01:42:19 -04:00
#include <string>
#include <iostream>
#include <sstream>
2021-05-08 20:01:53 -04:00
#include <ctime>
2019-04-23 01:42:19 -04:00
#define SDL_MAIN_HANDLED
#include "SDL.h"
#include "SDL_mixer.h"
#include "SDL_ttf.h"
2019-04-23 01:42:19 -04:00
#define GL_GLEXT_PROTOTYPES
#define GLEW_STATIC
#if defined(__EMSCRIPTEN__)
#include <emscripten.h>
#include <emscripten/html5.h>
#include <GL/glew.h>
#else
2019-04-29 19:27:13 -04:00
#include "glew/glew.h"
#endif
2019-04-23 01:42:19 -04:00
#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"
2019-06-16 03:05:02 -04:00
struct FramerateIndicator : Sprite
{
2019-06-16 03:05:02 -04:00
FramerateIndicator(Node*);
void respond(SDL_Event&);
SDL_Surface* get_surface();
void refresh();
2019-06-16 03:05:02 -04:00
};
2019-04-23 01:42:19 -04:00
class Game : public Node
2019-04-23 01:42:19 -04:00
{
private:
static void sdl_log_override(void*, int, SDL_LogPriority, const char*);
public:
2019-04-23 01:42:19 -04:00
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;
2019-05-07 03:33:54 -04:00
SDL_Window* window;
SDL_Renderer* renderer = NULL;
2019-04-23 01:42:19 -04:00
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);
2019-04-23 01:42:19 -04:00
Game();
~Game();
virtual void reset() { activate(); }
void print_error(const std::string&);
void print_sdl_error(const std::string&);
2019-04-23 01:42:19 -04:00
void print_gl_attributes();
void print_frame_length_history();
2019-04-23 01:42:19 -04:00
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();
2019-06-16 03:05:02 -04:00
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);
2019-04-23 01:42:19 -04:00
void run();
void frame(float);
2019-04-23 01:42:19 -04:00
void flag_to_end();
2019-05-03 02:09:48 -04:00
virtual void update() {};
2019-04-23 01:42:19 -04:00
void set_framerate(int);
2019-05-04 03:25:35 -04:00
void handle_quit_event(SDL_Event&);
2019-04-23 01:42:19 -04:00
void quit();
virtual std::string get_class_name() const { return "Game"; }
2019-04-23 01:42:19 -04:00
template<typename T>
float weight(T amount)
2019-04-23 01:42:19 -04:00
{
return (last_frame_length / (1000.0 / 60)) * amount;
}
};
#if defined(__EMSCRIPTEN__)
void loop(void*);
#endif
2019-04-23 01:42:19 -04:00
#endif