aboutsummaryrefslogtreecommitdiff
path: root/src/main.cpp
blob: ebf018da96811d70b50cd3150a7e33abd24fb675 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
#include "renderer/Renderer.hpp"
#include "games/Game.hpp"
#include "common/MemoryManager.hpp"

#include <glad/gl.h>
#include <SDL3/SDL.h>
#include <SDL3/SDL_video.h>

#include <imgui.h>
#include <imgui_impl_opengl3.h>
#include <imgui_impl_sdl3.h>

#include <iostream>
#include <memory>
#include <cstdlib>
#include <assert.h>
#include <chrono>


Game::GameType
DrawGameMenu()
{
    Game::GameType type = Game::no_game;
    ImGuiWindowFlags flags = 0;

    ImVec2 pos = {100, 200};
    ImGui::SetNextWindowPos(pos);


    ImGui::Begin("Game Selection", nullptr, flags);
    if (ImGui::Button("Tetris")) {
        type = Game::tetris;
    }
    if (ImGui::Button("Minesweeper")) {
        type = Game::minesweeper;
    }
    if (ImGui::Button("Snake")) {
        type = Game::snake;
    }
    if (ImGui::Button("Breakout")) {
        type = Game::breakout;
    }
    ImGui::End();

    return type;
}


void
DrawPerformanceInfo(float ms_used)
{
    ImGuiIO& io = ImGui::GetIO();
    ImGuiWindowFlags flags = ImGuiWindowFlags_NoNav | ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoCollapse | ImGuiWindowFlags_NoBackground | ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoMove;

    float ms_per_frame = 1000.0f / io.Framerate;
    float load = (ms_used / ms_per_frame) * 100;

    ImGui::SetNextWindowPos(ImVec2(io.DisplaySize.x - 350, 0));
    ImGui::Begin("Performance", nullptr, flags);
    ImGui::Text("%.2f ms/frame (%.2f %%) | %.2f ms/frame (%.f FPS)", ms_used, load, ms_per_frame, io.Framerate);
    ImGui::End();
}


SDL_Window*
CreateWindow()
{
    if (!SDL_Init(SDL_INIT_VIDEO | SDL_INIT_GAMEPAD)) {
        std::cerr << "Failed to init SDL3: " << SDL_GetError() << '\n';
        return nullptr;
    }


    const char* glsl_version = "#version 330";
    SDL_GL_SetAttribute(SDL_GL_CONTEXT_FLAGS, 0);
    SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_COMPATIBILITY);
    SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
    SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 3);

    SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
    SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);
    SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 8);


    SDL_Window* window = SDL_CreateWindow("fsarcade", 1280, 960, SDL_WINDOW_OPENGL | SDL_WINDOW_HIDDEN | SDL_WINDOW_HIGH_PIXEL_DENSITY);
    if (!window) {
        std::cerr << "Failed to create SDL_window: " << SDL_GetError() << '\n';
        return nullptr;
    }

    SDL_GLContext sdl_gl_context = SDL_GL_CreateContext(window);
    if (!sdl_gl_context) {
        std::cerr << "Failed to create SDL_GLContext: " << SDL_GetError() << '\n';
        SDL_DestroyWindow(window);
        return nullptr;
    }
    SDL_GL_MakeCurrent(window, sdl_gl_context);
    SDL_GL_SetSwapInterval(1); // enable vsync


    if (!gladLoadGL(SDL_GL_GetProcAddress)) {
        SDL_GL_DestroyContext(sdl_gl_context);
        SDL_DestroyWindow(window);
        std::cerr << "Failed to init GLAD!\n";
        return nullptr;
    }

    SDL_SetWindowPosition(window, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED);
    SDL_ShowWindow(window);


    IMGUI_CHECKVERSION();
    ImGui::CreateContext();
    ImGuiIO& io = ImGui::GetIO();
    io.IniFilename = NULL;
    io.LogFilename = NULL;
    io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard;
    io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad;

    ImGui::StyleColorsDark();
    ImGui_ImplSDL3_InitForOpenGL(window, sdl_gl_context);
    ImGui_ImplOpenGL3_Init(glsl_version);

    return window;
}


int
main(int argc, char** argv)
{
    SDL_Window* window = CreateWindow();
    if (!window) {
        return 0;
    }


    g_renderer.Init(window);
    std::unique_ptr<Game> game = nullptr;


    std::vector<SDL_Event> game_events;
    game_events.reserve(32);


    for (;;) {
        ImGui_ImplOpenGL3_NewFrame();
        ImGui_ImplSDL3_NewFrame();
        ImGui::NewFrame();


        auto start_time = std::chrono::high_resolution_clock::now();

        size_t cur_game_events = 0;
        size_t max_game_events = game_events.max_size();

        SDL_Event event;
        while (cur_game_events < max_game_events && SDL_PollEvent(&event)) {
            if (event.type == SDL_EVENT_KEY_DOWN ||
                event.type == SDL_EVENT_KEY_UP ||
                event.type == SDL_EVENT_MOUSE_BUTTON_DOWN)
            {
                game_events.emplace_back(event);
                cur_game_events++;
            }
            ImGui_ImplSDL3_ProcessEvent(&event);

            if (event.type == SDL_EVENT_QUIT)
                return 0;
            if (event.type == SDL_EVENT_WINDOW_CLOSE_REQUESTED && event.window.windowID == SDL_GetWindowID(window))
                return 0;
            if (event.type == SDL_EVENT_WINDOW_DESTROYED && event.window.windowID == SDL_GetWindowID(window)) {
                return 0;
            }
        }


        int w, h;
        SDL_GetWindowSize(window, &w, &h);
        g_renderer.SetScreenSize(w, h);
        g_renderer.SetClearColor({0.3f, 0.3f, 0.3f});
        g_renderer.SetCameraSize(4.0f, 3.0f);


        if (game) {
            bool keep_game_running = game->Update(game_events);
            if (!keep_game_running) {
                game.reset();
            }
        }
        else {
            Game::GameType type = DrawGameMenu();
            if (type != Game::no_game) {
                game = Game::Select(type);
            }
        }

        game_events.clear();
        g_renderer.Draw();

        auto end_time = std::chrono::high_resolution_clock::now();
        float ms_used = float((end_time - start_time) / std::chrono::microseconds(1)) / 1000;
        DrawPerformanceInfo(ms_used);


        ImGui::Render();
        ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());


        SDL_GL_SwapWindow(window);
        g_renderer.Reset();
        MemoryManager::Clear_Frame();
    }

    return 0;
}