Advertisement
Guest User

Untitled

a guest
May 20th, 2018
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.68 KB | None | 0 0
  1. #include <SDL2/SDL.h>
  2. #include <stdio.h>
  3. #include <unistd.h>
  4. #include <string.h>
  5. #include <fcntl.h>
  6. #include <sys/ioctl.h>
  7. #include <stdint.h>
  8. #include <unistd.h>
  9.  
  10. #include <stdlib.h>
  11. #include <time.h>
  12.  
  13. #include "doomdev.h"
  14. #include <errno.h>
  15.  
  16. #define WIDTH 640
  17. #define HEIGHT 480
  18.  
  19. #define COLOR(r, g, b) (((r)&0b11100000) | ((g)>>6<<3) | ((b) >> 5))
  20.  
  21. #define FIXED_POINT(a, b) ((a<<16)|(b))
  22.  
  23. // gcc -std=c11 -lSDL2
  24.  
  25. int random(int min, int max)
  26. {
  27. return rand() % (max-min) + min;
  28. }
  29.  
  30. void blit_texture(uint8_t* screen, SDL_Renderer* renderer, SDL_Color* colors)
  31. {
  32. SDL_Surface* surf = SDL_CreateRGBSurfaceFrom(screen, WIDTH, HEIGHT, 8, WIDTH, 0, 0, 0, 0);
  33. SDL_SetPaletteColors(surf->format->palette, colors, 0, 256);
  34. SDL_Texture* tex = SDL_CreateTextureFromSurface(renderer, surf);
  35. SDL_FreeSurface(surf);
  36.  
  37. SDL_RenderCopy(renderer, tex, NULL, NULL);
  38. SDL_RenderPresent(renderer);
  39.  
  40. SDL_DestroyTexture(tex);
  41. }
  42.  
  43. void draw_line(int fd, int ax, int ay, int bx, int by, int color)
  44. {
  45. struct doomdev_line lines[1] = {
  46. { ax, ay, bx, by, color, 0}
  47. };
  48.  
  49. struct doomdev_surf_ioctl_draw_lines params = {
  50. .lines_ptr = (uint64_t)lines,
  51. .lines_num = 1,
  52. };
  53.  
  54. int res = ioctl(fd, DOOMDEV_SURF_IOCTL_DRAW_LINES, &params);
  55. }
  56.  
  57. void fill_rect(int fd, int ax, int ay, int width, int height, int color)
  58. {
  59. struct doomdev_fill_rect data[1] = {
  60. { ax, ay, width, height, color, 0}
  61. };
  62.  
  63. struct doomdev_surf_ioctl_fill_rects params = {
  64. .rects_ptr = (uint64_t)data,
  65. .rects_num = 1,
  66. };
  67.  
  68. int res = ioctl(fd, DOOMDEV_SURF_IOCTL_FILL_RECTS, &params);
  69. }
  70.  
  71. int create_surface(int fd, int width, int height)
  72. {
  73. struct doomdev_ioctl_create_surface param = {
  74. .width = width,
  75. .height = height,
  76. };
  77.  
  78. int sfd = ioctl(fd, DOOMDEV_IOCTL_CREATE_SURFACE, &param);
  79. if (sfd < 0) {
  80. printf("doomdev create_surface fail\n");
  81. }
  82. return sfd;
  83. }
  84.  
  85. int create_flat_tex(int fd, char* data)
  86. {
  87. struct doomdev_ioctl_create_flat param = {
  88. .data_ptr = (uint64_t)data
  89. };
  90.  
  91. int sfd = ioctl(fd, DOOMDEV_IOCTL_CREATE_FLAT, &param);
  92. if (sfd < 0) {
  93. printf("doomdev create_flat_tex fail\n");
  94. }
  95. return sfd;
  96. }
  97.  
  98. int create_texure(int fd, int size, int height, char* data)
  99. {
  100. struct doomdev_ioctl_create_texture param = {
  101. .data_ptr = (uint64_t)data,
  102. .size = size,
  103. .height = height
  104. };
  105.  
  106. int sfd = ioctl(fd, DOOMDEV_IOCTL_CREATE_TEXTURE, &param);
  107. if (sfd < 0) {
  108. printf("doomdev create_tex fail\n");
  109. }
  110. return sfd;
  111. }
  112.  
  113. int draw_background(int fd, int tex)
  114. {
  115. struct doomdev_surf_ioctl_draw_background params = {
  116. .flat_fd = tex
  117. };
  118.  
  119. int res = ioctl(fd, DOOMDEV_SURF_IOCTL_DRAW_BACKGROUND, &params);
  120.  
  121. return res;
  122. }
  123.  
  124. int copy_rect(int fd, int src, int ax, int ay, int destx, int desty, int width, int height)
  125. {
  126. struct doomdev_copy_rect data[1] = {
  127. { destx, desty, ax, ay, width, height}
  128. };
  129.  
  130. struct doomdev_surf_ioctl_copy_rects params = {
  131. .rects_ptr = (uint64_t)data,
  132. .surf_src_fd = src,
  133. .rects_num = 1,
  134. };
  135.  
  136. int res = ioctl(fd, DOOMDEV_SURF_IOCTL_COPY_RECTS, &params);
  137.  
  138. return res;
  139. }
  140.  
  141. int draw_column(int fd, int flags, int texture, int translation, int colormap, int translation_idx, int offset, uint32_t ustart, uint32_t ustep, int x, int y1, int y2, int colormap_idx)
  142. {
  143. struct doomdev_column data[1] = {
  144. {
  145. .texture_offset = offset,
  146. .ustart = ustart,
  147. .ustep = ustep,
  148. .y1 = y1,
  149. .y2 = y2,
  150. .x = x,
  151. .colormap_idx = colormap_idx
  152. }
  153. };
  154.  
  155. struct doomdev_surf_ioctl_draw_columns params = {
  156. .columns_ptr = (uint64_t)data,
  157. .texture_fd = texture,
  158. .translations_fd = translation,
  159. .colormaps_fd = colormap,
  160. .columns_num = 1,
  161. .draw_flags = flags,
  162. .translation_idx = translation_idx
  163. };
  164.  
  165. return ioctl(fd, DOOMDEV_SURF_IOCTL_DRAW_COLUMNS, &params);
  166. }
  167.  
  168. void fetch_surf(uint8_t* buf, int fd)
  169. {
  170. pread(fd, buf, WIDTH * HEIGHT, 0);
  171. }
  172.  
  173. int sdl_should_continue()
  174. {
  175. SDL_Event Events;
  176. while (SDL_PollEvent(&Events))
  177. {
  178. if (Events.type == SDL_QUIT)
  179. return 0;
  180. }
  181. return 1;
  182. }
  183.  
  184. int main (int argc, char** argv)
  185. {
  186. uint8_t screen[WIDTH*HEIGHT];
  187. SDL_Event Events;
  188.  
  189.  
  190. srand(NULL);
  191. SDL_Window* window = NULL;
  192. window = SDL_CreateWindow
  193. (
  194. "", SDL_WINDOWPOS_UNDEFINED,
  195. SDL_WINDOWPOS_UNDEFINED,
  196. WIDTH,
  197. HEIGHT,
  198. SDL_WINDOW_SHOWN
  199. );
  200.  
  201. // Setup renderer
  202. SDL_Renderer* renderer = NULL;
  203. renderer = SDL_CreateRenderer( window, -1, SDL_RENDERER_ACCELERATED);
  204.  
  205. SDL_Color colors[256];
  206. for (int i = 0; i < 256; i++)
  207. {
  208. colors[i].r = i & 0b11100000;
  209. colors[i].g = (i & 0b00011000) << 3;
  210. colors[i].b = (i & 0b00000111) << 5;
  211. }
  212.  
  213.  
  214.  
  215.  
  216. // doom
  217. int doom_fd = open("/dev/doom0", O_RDWR);
  218. if (doom_fd < 0) {
  219. printf("error doom_fd: %d\n", errno);
  220. return -1;
  221. }
  222.  
  223. int framebuffer = create_surface(doom_fd, WIDTH, HEIGHT);
  224. int surf = create_surface(doom_fd, WIDTH, HEIGHT);
  225.  
  226. unsigned char image[64*64];
  227. for (int i = 0; i < 64*64; ++i)
  228. {
  229. int r = 0;//i*255/64/64;
  230. int g = 255;//*255/64/64;
  231. int b =0;
  232. image[i] = COLOR(r, g, b);
  233. }
  234.  
  235. int tex = create_flat_tex(doom_fd, image);
  236.  
  237. int tex2 = create_texure(doom_fd, 64*64, 64, image);
  238.  
  239. while (sdl_should_continue())
  240. {
  241.  
  242. //draw_background(surf, tex);
  243.  
  244. //draw_line(surf, random(0, WIDTH-1), random(0, HEIGHT-1), random(0, WIDTH-1), random(0, HEIGHT-1), random(0, 255));
  245.  
  246.  
  247. int ax = random(0, WIDTH-2);
  248. int ay = random(0, HEIGHT-2);
  249. int w = random(0, WIDTH-1-ax);
  250. int h = random(0, HEIGHT-1-ay);
  251.  
  252. //fill_rect(framebuffer, ax, ay, w, h, random(0, 255));
  253.  
  254. for (int j = 0; j < 20; ++j)
  255. {
  256. draw_column(framebuffer, 0, tex2, 0, 0, 0, 0, FIXED_POINT(0, 0), FIXED_POINT(0, 1), 50+j, 0, 200, 0);
  257. }
  258.  
  259. // sleep(1);
  260.  
  261. // copy_rect(framebuffer, surf, 0, 0, 0, 0, WIDTH/2-1, HEIGHT-1);
  262. draw_line(framebuffer, 0, 0, 640-1, 480-1, 128);
  263.  
  264. sleep(1);
  265.  
  266. fetch_surf(screen, framebuffer);
  267. blit_texture(screen, renderer, colors);
  268.  
  269. break;
  270. //usleep(6000); // 20
  271. }
  272.  
  273. sleep(5);
  274.  
  275. SDL_DestroyWindow(window);
  276. SDL_Quit();
  277.  
  278. return EXIT_SUCCESS;
  279. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement