-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwindow.cpp
More file actions
53 lines (48 loc) · 1.69 KB
/
window.cpp
File metadata and controls
53 lines (48 loc) · 1.69 KB
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
#include "header.hpp"
Win *new_window(int width, int height, char *title)
{
Win *win;
win = (Win *)calloc(1, sizeof(Win));
win->width = width;
win->height = height;
win->scene.sum = (Color *)calloc(win->width * win->height, sizeof(Color));
win->pixels = (uint32_t *)calloc(width * height, sizeof(uint32_t));
win->scene.camera = (Vec3){0, 1.5, 5};
if (SDL_Init(SDL_INIT_VIDEO) < 0)
{
std::cerr << "Error opening window" << std::endl;
SDL_Quit();
exit(-1);
}
win->window = SDL_CreateWindow(title, 0, 0, width, height, SDL_WINDOW_SHOWN);
if (win->window == NULL)
{
std::cerr << "Error opening window" << std::endl;
SDL_Quit();
exit(-1);
}
win->renderer = SDL_CreateRenderer(win->window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
SDL_SetWindowMinimumSize(win->window, width, height);
SDL_RenderSetLogicalSize(win->renderer, width, height);
SDL_RenderSetIntegerScale(win->renderer, (SDL_bool)1);
win->screen_texture = SDL_CreateTexture(win->renderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_STREAMING, width, height);
return win;
}
void update_window(Win *win)
{
SDL_UpdateTexture(win->screen_texture, NULL, win->pixels, win->width * 4);
SDL_RenderCopy(win->renderer, win->screen_texture, NULL, NULL);
SDL_RenderPresent(win->renderer);
}
void close_window(Win *win)
{
free(win->pixels);
for (int i = 0; i < win->scene.pos; i++)
free(win->scene.objects[i]);
free(win->scene.objects);
free(win->scene.sum);
SDL_DestroyTexture(win->screen_texture);
SDL_DestroyRenderer(win->renderer);
SDL_DestroyWindow(win->window);
SDL_Quit();
}