spacebox/src/Game.hpp

109 lines
2.4 KiB
C++

#ifndef Game_h_
#define Game_h_
#include <string>
#include <iostream>
#include <list>
#include <sstream>
#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 "Recorder.hpp"
#include "Sprite.hpp"
struct FramerateIndicator : Sprite
{
FramerateIndicator(Node*);
void respond(SDL_Event&);
SDL_Surface* get_surface();
void refresh();
};
struct Game : Node
{
Game(const Game&) = delete;
Game& operator=(const Game&) = delete;
Game(Game&&) = delete;
Game& operator=(Game&&) = delete;
SDL_Window* window;
SDL_Renderer* renderer = NULL;
SDL_GLContext glcontext = NULL;
// 768, 432
// 864, 486
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);
std::vector<float> frame_length_history;
TTF_Font* bp_mono_font = NULL;
FramerateIndicator framerate_indicator = FramerateIndicator(this);
Game();
~Game();
void print_error(std::string);
void print_sdl_error(std::string);
void print_gl_attributes();
void print_frame_length_history();
void load_sdl_context();
void load_gl_context();
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);
SDL_Window* get_window();
SDL_Renderer* get_renderer();
void run();
void frame(float);
void flag_to_end();
virtual void update() {};
glm::vec2 weight(glm::vec2);
void set_framerate(int);
void handle_quit_event(SDL_Event&);
void quit();
std::string get_class_name() { 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