-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgraphics-cuda.h
109 lines (96 loc) · 5.08 KB
/
graphics-cuda.h
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
#ifndef GRAPHICS_CUDA_H
#define GRAPHICS_CUDA_H
#pragma once
#ifndef NO_SDL
#include <SDL2/SDL.h>
#include <stdint.h>
#include "colours.h"
uint32_t *gpuAlloc(unsigned screen_size);
void gpuFree(void *gpu_mem);
unsigned gpuBlit(void *src, void *dst, unsigned screen_size);
int render(SDL_Surface *screen,
void *cuda_pixels,
unsigned *field, unsigned S, unsigned H, unsigned W);
#define SDL_BOILERPLATE \
window = SDL_CreateWindow( \
"main", \
SDL_WINDOWPOS_UNDEFINED, \
SDL_WINDOWPOS_UNDEFINED, \
W * S, H * S, \
SDL_WINDOW_SHOWN); \
if (window == NULL) { \
cerr << "could not create window: " \
<< SDL_GetError() \
<< "Reverting to terminal." \
<< endl; \
S = 0; \
} \
screen = SDL_CreateRGBSurface(0, W * S, H * S, 32, \
R_MASK, \
G_MASK, \
B_MASK, \
A_MASK); \
if (screen == NULL) { \
SDL_Log("SDL_CreateRGBSurface() failed: %s.", SDL_GetError()); \
S = 0; \
} \
renderer = SDL_CreateRenderer(window, -1, \
SDL_RENDERER_ACCELERATED | \
SDL_RENDERER_PRESENTVSYNC); \
texture = SDL_CreateTexture(renderer, \
SDL_PIXELFORMAT_ARGB8888, \
SDL_TEXTUREACCESS_STREAMING | \
SDL_TEXTUREACCESS_TARGET, \
W * S, H * S); \
graphics_buffer = gpuAlloc(H * W * S * S); \
if (graphics_buffer == NULL) { \
cerr << "failed to alloc gpu memory; reverting to terminal" << endl; \
S = 0; \
}
#define SDL_CHECK_INIT \
if (SDL_Init(SDL_INIT_EVERYTHING) < 0) { \
cerr << "could not initialize sdl2: " << SDL_GetError() << endl; \
return 1; \
}
#define SDL_DESTROY_QUIT \
SDL_DestroyTexture(texture); \
SDL_DestroyRenderer(renderer); \
SDL_DestroyWindow(window); \
SDL_Quit();
#define SDL_DRAW \
SDL_LockSurface(screen); \
if (render(screen, graphics_buffer, field, S, H, W) != 0) { \
cerr << "cuda error" << endl; \
S = 0; \
continue; \
} \
SDL_UnlockSurface(screen); \
SDL_UpdateTexture(texture, NULL, \
screen->pixels, \
screen->pitch); \
SDL_RenderClear(renderer); \
SDL_RenderCopy(renderer, texture, NULL, NULL); \
SDL_RenderPresent(renderer);
#define SDL_QUIT_EVENT_HANDLER \
SDL_Event event; \
if (SDL_PollEvent(&event) && \
(event.type == SDL_QUIT || \
(event.type == SDL_KEYDOWN && \
event.key.keysym.sym == 'q'))) \
break;
#define SDL_WINDOW \
SDL_Renderer *renderer; \
SDL_Surface *screen; \
SDL_Texture *texture; \
SDL_Window *window; \
\
uint32_t *graphics_buffer;
#else
#define SDL_BOILERPLATE ;
#define SDL_CHECK_INIT ;
#define SDL_DESTROY_QUIT ;
#define SDL_DRAW ;
#define SDL_QUIT_EVENT_HANDLER ;
#define SDL_WINDOW ;
#endif
#endif