-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnative_main.cpp
130 lines (104 loc) · 3.39 KB
/
native_main.cpp
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
#include <unistd.h>
#include <iostream>
#include <chrono>
#include <thread>
#include <barrier>
#include <condition_variable>
#include <lodepng.h>
#include <time.h>
#include <SDL2/SDL.h>
#include "common.h"
#include "mathematics.h"
#include "rendering/renderer.h"
#include "rendering/postprocessing.h"
#include "hardware/input.h"
#include "time.hpp"
extern void game_init();
extern void game_update();
extern void game_mesh_render();
extern void game_ui_render();
SDL_Window* setupWindow(){
if(SDL_Init(SDL_INIT_VIDEO) == -1)
{
std::cout << "SDL failed to init: " << SDL_GetError() << "\n";
exit(1);
}
SDL_Window* window = SDL_CreateWindow("Native", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
if(window == NULL)
{
std::cout << "SDL failed to create window: " << SDL_GetError() << "\n";
exit(1);
}
return window;
}
std::mutex barrierStartMutex;
std::barrier renderStartBarrier{2};
std::mutex barrierDoneMutex;
std::barrier renderDoneBarrier{2};
void core1(){
while(true){
renderStartBarrier.arrive_and_wait();
while(Renderer::Render());
Renderer::Finish();
}
}
int main(int argc, char** argv){
SDL_Window* window = setupWindow();
SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_PRESENTVSYNC);
SDL_SetWindowMinimumSize(window, FRAME_WIDTH, FRAME_HEIGHT);
SDL_RenderSetLogicalSize(renderer, FRAME_WIDTH, FRAME_HEIGHT);
SDL_RenderSetIntegerScale(renderer, SDL_TRUE);
// 4 bits per pixel
SDL_Surface *surface = SDL_CreateRGBSurfaceWithFormat(0, FRAME_WIDTH, FRAME_HEIGHT, 32, SDL_PIXELFORMAT_RGBA32);
if(!surface){
printf("Failed to create surface\n");
return 1;
}
SDL_SetRelativeMouseMode(SDL_TRUE);
Time::Init();
Input::Init();
Renderer::Init();
game_init();
std::thread secondThread(core1);
while(true){
Time::Tick();
Input::Poll();
SDL_Event event;
while(SDL_PollEvent(&event)){
switch(event.type){
case SDL_QUIT:
SDL_DestroyWindow(window);
exit(0);
break;
default:
Input::HandleEvent(&event);
}
}
game_update();
Renderer::Prepare();
renderStartBarrier.arrive_and_wait();
Time::Profiler::Enter("DrawMesh");
game_mesh_render();
while(Renderer::Render());
Renderer::Finish();
Time::Profiler::Exit("DrawMesh");
Time::Profiler::Enter("PostProcessing");
PostProcessing::Apply((Color565*)&Renderer::FrameBuffer, vec2i16(120, 120));
Time::Profiler::Exit("PostProcessing");
game_ui_render();
SDL_LockSurface(surface);
static Color pixels[FRAME_WIDTH * FRAME_HEIGHT];
for(int i = 0; i < FRAME_WIDTH * FRAME_HEIGHT; i++){
pixels[i] = Color(Renderer::FrameBuffer[i]);
}
memcpy(surface->pixels, &pixels, FRAME_WIDTH * FRAME_HEIGHT * sizeof(Color));
SDL_UnlockSurface(surface);
SDL_RenderClear(renderer);
SDL_Texture *texture = SDL_CreateTextureFromSurface(renderer, surface);
SDL_RenderCopy(renderer, texture, NULL, NULL);
SDL_RenderPresent(renderer);
SDL_DestroyTexture(texture);
}
SDL_DestroyWindow(window);
return 0;
}