graphics.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  1. /*
  2. * Graphic Manager - The peTI-NESulator Project
  3. * os/macos/graphics.c
  4. *
  5. * Created by Manoël Trapier on 08/05/08.
  6. * Copyright (c) 2002-2019 986-Studio.
  7. *
  8. */
  9. #include <stdlib.h>
  10. #include <stdio.h>
  11. #include <string.h>
  12. #include <stdint.h>
  13. #include <os_dependent.h>
  14. #define GLFW_INCLUDE_GLEXT
  15. #include <GLFW/glfw3.h>
  16. /* "Apple" fix */
  17. #ifndef GL_TEXTURE_RECTANGLE
  18. #define GL_TEXTURE_RECTANGLE GL_TEXTURE_RECTANGLE_EXT
  19. #endif
  20. #include <palette.h>
  21. typedef struct GLWindow_t GLWindow;
  22. struct KeyArray
  23. {
  24. uint8_t lastState;
  25. uint8_t curState;
  26. uint8_t debounced;
  27. GLFWwindow *window;
  28. };
  29. struct GLWindow_t
  30. {
  31. struct KeyArray keyArray[512];
  32. GLFWwindow *windows;
  33. uint8_t *videoMemory;
  34. GLuint videoTexture;
  35. int WIDTH;
  36. int HEIGHT;
  37. };
  38. static int window_num = 0;
  39. void GLWindowInitEx(GLWindow *g, int w, int h)
  40. {
  41. g->WIDTH = w;
  42. g->HEIGHT = h;
  43. g->videoTexture = window_num++;
  44. }
  45. void GLWindowInit(GLWindow *g)
  46. {
  47. GLWindowInitEx(g, 100, 100);
  48. }
  49. void ShowScreen(GLWindow *g, int w, int h)
  50. {
  51. glBindTexture(GL_TEXTURE_RECTANGLE, g->videoTexture);
  52. // glTexSubImage2D is faster when not using a texture range
  53. glTexSubImage2D(GL_TEXTURE_RECTANGLE, 0, 0, 0, w, h, GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, g->videoMemory);
  54. glBegin(GL_QUADS);
  55. glTexCoord2f(0.0f, 0.0f);
  56. glVertex2f(-1.0f, 1.0f);
  57. glTexCoord2f(0.0f, h);
  58. glVertex2f(-1.0f, -1.0f);
  59. glTexCoord2f(w, h);
  60. glVertex2f(1.0f, -1.0f);
  61. glTexCoord2f(w, 0.0f);
  62. glVertex2f(1.0f, 1.0f);
  63. glEnd();
  64. glFlush();
  65. }
  66. void setupGL(GLWindow *g, int w, int h)
  67. {
  68. g->videoMemory = (uint8_t *)malloc(w * h * sizeof(uint32_t));
  69. memset(g->videoMemory, 0, w * h * sizeof(uint32_t));
  70. //Tell OpenGL how to convert from coordinates to pixel values
  71. glViewport(0, 0, w, h);
  72. glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  73. glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
  74. glClearColor(1.0f, 0.f, 1.0f, 1.0f);
  75. glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
  76. glMatrixMode(GL_PROJECTION);
  77. glLoadIdentity();
  78. glMatrixMode(GL_MODELVIEW);
  79. glLoadIdentity();
  80. glDisable(GL_TEXTURE_2D);
  81. glEnable(GL_TEXTURE_RECTANGLE);
  82. glBindTexture(GL_TEXTURE_RECTANGLE, g->videoTexture);
  83. // glTextureRangeAPPLE(GL_TEXTURE_RECTANGLE_NV_EXT, 0, NULL);
  84. // glTexParameteri(GL_TEXTURE_RECTANGLE_NV_EXT, GL_TEXTURE_STORAGE_HINT_APPLE , GL_STORAGE_CACHED_APPLE);
  85. // glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_TRUE);
  86. glTexParameteri(GL_TEXTURE_RECTANGLE, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  87. glTexParameteri(GL_TEXTURE_RECTANGLE, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  88. glTexParameteri(GL_TEXTURE_RECTANGLE, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  89. glTexParameteri(GL_TEXTURE_RECTANGLE, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  90. glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
  91. glTexImage2D(GL_TEXTURE_RECTANGLE, 0, GL_RGBA, w,
  92. h, 0, GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, g->videoMemory);
  93. glDisable(GL_DEPTH_TEST);
  94. }
  95. void restoreGL(GLWindow *g, int w, int h)
  96. {
  97. //Tell OpenGL how to convert from coordinates to pixel values
  98. glViewport(0, 0, w, h);
  99. glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  100. glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
  101. glClearColor(1.0f, 0.f, 1.0f, 1.0f);
  102. glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
  103. glClear(GL_COLOR_BUFFER_BIT);
  104. glMatrixMode(GL_PROJECTION);
  105. glLoadIdentity();
  106. glMatrixMode(GL_MODELVIEW);
  107. glLoadIdentity();
  108. glDisable(GL_TEXTURE_2D);
  109. glEnable(GL_TEXTURE_RECTANGLE);
  110. glDisable(GL_DEPTH_TEST);
  111. }
  112. void kbHandler(GLFWwindow *window, int key, int scan, int action, int mod)
  113. {
  114. struct KeyArray *keyArray;
  115. keyArray = (struct KeyArray *)glfwGetWindowUserPointer(window);
  116. keyArray[key].lastState = keyArray[key].curState;
  117. if (action == GLFW_RELEASE)
  118. {
  119. keyArray[key].curState = GLFW_RELEASE;
  120. }
  121. else
  122. {
  123. keyArray[key].curState = GLFW_PRESS;
  124. }
  125. keyArray[key].debounced |= (keyArray[key].lastState == GLFW_RELEASE) && (keyArray[key].curState == GLFW_PRESS);
  126. keyArray[key].window = window;
  127. /*printf("key:%d, state:%d debounce:%d, laststate:%d\n", key, keyArray[key].curState,
  128. keyArray[key].debounced, keyArray[key].lastState);*/
  129. }
  130. void sizeHandler(GLFWwindow *window, int xs, int ys)
  131. {
  132. glfwMakeContextCurrent(window);
  133. glViewport(0, 0, xs, ys);
  134. }
  135. static void error_callback(int error, const char *description)
  136. {
  137. puts(description);
  138. }
  139. void initDisplay(GLWindow *g)
  140. {
  141. int h = g->HEIGHT;
  142. int w = g->WIDTH;
  143. /// Initialize GLFW
  144. glfwInit();
  145. glfwSetErrorCallback(error_callback);
  146. // Open screen OpenGL window
  147. if (!(g->windows = glfwCreateWindow(g->WIDTH, g->HEIGHT, "Main", NULL, NULL)))
  148. {
  149. glfwTerminate();
  150. fprintf(stderr, "Window creation error...\n");
  151. abort();
  152. }
  153. glfwSetWindowAspectRatio(g->windows, 4, 3);
  154. glfwMakeContextCurrent(g->windows);
  155. setupGL(g, g->WIDTH, g->HEIGHT);
  156. glfwSwapInterval(1); // We need vsync
  157. glfwGetWindowSize(g->windows, &w, &h);
  158. glfwSetWindowUserPointer(g->windows, g->keyArray);
  159. glfwSetKeyCallback(g->windows, kbHandler);
  160. glfwSetWindowSizeCallback(g->windows, sizeHandler);
  161. }
  162. static void drawPixel(GLWindow *gw, uint32_t x, uint32_t y, uint32_t colour)
  163. {
  164. uint8_t r, g, b, a;
  165. uint32_t offset = (y * gw->WIDTH * 4U) + 4U * x;
  166. if ((x > (uint32_t)gw->WIDTH) || (y > (uint32_t)gw->HEIGHT))
  167. {
  168. return;
  169. }
  170. b = colour & 0xFF;
  171. g = (colour >> 8) & 0xFF;
  172. r = (colour >> 16) & 0xFF;
  173. a = (colour >> 24) & 0xFF;
  174. gw->videoMemory[offset + 0] = a;
  175. gw->videoMemory[offset + 1] = r;
  176. gw->videoMemory[offset + 2] = g;
  177. gw->videoMemory[offset + 3] = b;
  178. }
  179. static void drawLine(GLWindow *g, int32_t x0, int32_t y0, int32_t x1, int32_t y1, int32_t colour)
  180. {
  181. printf("%s:%s(%p, %d, %d, %d, %d, %d) @ %d\n", __FILE__, __func__,
  182. g, x0, y0, x1, y1, colour,
  183. __LINE__);
  184. int32_t d, dx, dy, aincr, bincr, xincr, yincr;
  185. int32_t x, y;
  186. if (abs((int32_t)x1 - x0) < abs((int32_t)y1 - y0))
  187. {
  188. /* parcours par l'axe vertical */
  189. if (y0 > y1)
  190. {
  191. drawLine(g, x1, y1, x0, y0, colour);
  192. goto exit;
  193. }
  194. xincr = x1 > x0 ? 1 : -1;
  195. dy = y1 - y0;
  196. dx = abs((int32_t)x1 - x0);
  197. d = 2 * dx - dy;
  198. aincr = 2 * ((int32_t)dx - dy);
  199. bincr = 2 * dx;
  200. x = x0;
  201. y = y0;
  202. drawPixel(g, x, y, colour);
  203. for (y = y0 + 1 ; y <= y1 ; y++)
  204. {
  205. if (d >= 0)
  206. {
  207. x += xincr;
  208. d += aincr;
  209. }
  210. else
  211. {
  212. d += bincr;
  213. }
  214. drawPixel(g, x, y, colour);
  215. }
  216. }
  217. else
  218. {
  219. /* parcours par l'axe horizontal */
  220. if (x0 > x1)
  221. {
  222. drawLine(g, x1, y1, x0, y0, colour);
  223. goto exit;
  224. }
  225. yincr = y1 > y0 ? 1 : -1;
  226. dx = x1 - x0;
  227. dy = abs((int32_t)y1 - y0);
  228. d = 2 * dy - dx;
  229. aincr = 2 * (dy - dx);
  230. bincr = 2 * dy;
  231. x = x0;
  232. y = y0;
  233. drawPixel(g, x, y, colour);
  234. for (x = x0 + 1 ; x <= x1 ; ++x)
  235. {
  236. if (d >= 0)
  237. {
  238. y += yincr;
  239. d += aincr;
  240. }
  241. else
  242. {
  243. d += bincr;
  244. }
  245. drawPixel(g, x, y, colour);
  246. }
  247. }
  248. exit:
  249. return;
  250. }
  251. static void drawCircle(GLWindow *g, int xc, int yc, int radius, uint32_t colour)
  252. {
  253. int f = 1 - radius;
  254. int ddF_x = 0;
  255. int ddF_y = -2 * radius;
  256. int x = 0;
  257. int y = radius;
  258. int pX, pY;
  259. pX = xc;
  260. pY = yc + radius;
  261. drawPixel(g, pX, pY, colour);
  262. pY -= (2 * radius);
  263. drawPixel(g, pX, pY, colour);
  264. pY += radius;
  265. pX += radius;
  266. drawPixel(g, pX, pY, colour);
  267. pX -= (2 * radius);
  268. drawPixel(g, pX, pY, colour);
  269. while (x < y)
  270. {
  271. if (f >= 0)
  272. {
  273. y--;
  274. ddF_y += 2;
  275. f += ddF_y;
  276. }
  277. x++;
  278. ddF_x += 2;
  279. f += ddF_x + 1;
  280. pX = xc + x;
  281. pY = yc + y;
  282. drawPixel(g, pX, pY, colour);
  283. pX = xc - x;
  284. pY = yc + y;
  285. drawPixel(g, pX, pY, colour);
  286. pX = xc + x;
  287. pY = yc - y;
  288. drawPixel(g, pX, pY, colour);
  289. pX = xc - x;
  290. pY = yc - y;
  291. drawPixel(g, pX, pY, colour);
  292. pX = xc + y;
  293. pY = yc + x;
  294. drawPixel(g, pX, pY, colour);
  295. pX = xc - y;
  296. pY = yc + x;
  297. drawPixel(g, pX, pY, colour);
  298. pX = xc + y;
  299. pY = yc - x;
  300. drawPixel(g, pX, pY, colour);
  301. pX = xc - y;
  302. pY = yc - x;
  303. drawPixel(g, pX, pY, colour);
  304. }
  305. }
  306. static void drawRect(GLWindow *g, uint32_t x0, uint32_t y0, uint32_t w, uint32_t h, uint32_t colour)
  307. {
  308. printf("%s:%s(%p, %d, %d, %d, %d, %d) @ %d\n", __FILE__, __func__,
  309. g, x0, y0, w, h, colour,
  310. __LINE__);
  311. drawLine(g, x0, y0, x0 + w, y0, colour);
  312. drawLine(g, x0 + w, y0, x0 + w, y0 + h, colour);
  313. drawLine(g, x0 + w, y0 + h, x0, y0 + h, colour);
  314. drawLine(g, x0, y0 + h, x0, y0, colour);
  315. }
  316. static void drawFillrect(GLWindow *g, uint32_t x0, uint32_t y0, uint32_t w, uint32_t h, uint32_t colour)
  317. {
  318. printf("%s:%s(%p, %d, %d, %d, %d, %d) @ %d\n", __FILE__, __func__,
  319. g, x0, y0, w, h, colour,
  320. __LINE__);
  321. uint32_t i;
  322. for (i = 0 ; i < h ; i++)
  323. {
  324. drawLine(g, x0, y0 + i, x0 + w, y0 + i, colour);
  325. }
  326. }
  327. void clearScreen(GLWindow *g)
  328. {
  329. memset(g->videoMemory, 0, sizeof(uint8_t) * g->WIDTH * g->HEIGHT * 4);
  330. }
  331. void updateScreen(GLWindow *g)
  332. {
  333. /* Update windows code */
  334. glfwMakeContextCurrent(g->windows);
  335. ShowScreen(g, g->WIDTH, g->HEIGHT);
  336. glfwSwapBuffers(g->windows);
  337. glfwPollEvents();
  338. }
  339. void updateScreenAndWait(GLWindow *g)
  340. {
  341. while (glfwGetKey(g->windows, GLFW_KEY_ESCAPE) != GLFW_PRESS)
  342. {
  343. updateScreen(g);
  344. glfwPollEvents();
  345. }
  346. while (glfwGetKey(g->windows, GLFW_KEY_ESCAPE) != GLFW_RELEASE)
  347. {
  348. glfwPollEvents();
  349. }
  350. }
  351. GLWindow mainWindow;
  352. int graphics_init()
  353. {
  354. GLWindowInitEx(&mainWindow, 256, 240);
  355. initDisplay(&mainWindow);
  356. clearScreen(&mainWindow);
  357. updateScreen(&mainWindow);
  358. return 0;
  359. }
  360. static uint32_t getColour(long color)
  361. {
  362. Palette *pal = &basicPalette[color];
  363. uint8_t r, g, b, a;
  364. r = pal->r << 2;
  365. b = pal->b << 2;
  366. g = pal->g << 2;
  367. a = 255;//pal->a;
  368. return (b << 24) | (g << 16) | (r << 8) | a;
  369. }
  370. int graphics_getScreenSize(int *w, int *h)
  371. {
  372. *w = mainWindow.WIDTH;
  373. *h = mainWindow.HEIGHT;
  374. return 0;
  375. }
  376. int graphics_drawRect(uint32_t x0, uint32_t y0, uint32_t w, uint32_t h, uint32_t colour)
  377. {
  378. printf("%s:%s(%d, %d, %d, %d, %d) @ %d\n", __FILE__, __func__,
  379. x0, y0, w, h, colour,
  380. __LINE__);
  381. drawRect(&mainWindow, x0, y0, w, h, colour);
  382. return 0;
  383. }
  384. int graphics_drawFillrect(int x0, int y0, int w, int h, uint32_t colour)
  385. {
  386. printf("%s:%s(%d, %d, %d, %d, %d) @ %d\n", __FILE__, __func__,
  387. x0, y0, w, h, colour,
  388. __LINE__);
  389. drawFillrect(&mainWindow, x0, y0, w, h, colour);
  390. return 0;
  391. }
  392. int graphics_drawpixel(long x, long y, long color)
  393. {
  394. drawPixel(&mainWindow, x, y, getColour(color));
  395. return 0;
  396. }
  397. int graphics_drawCircle(int xc, int yc, int radius, uint32_t colour)
  398. {
  399. drawCircle(&mainWindow, xc, yc, radius, colour);
  400. return 0;
  401. }
  402. int graphics_drawline(uint32_t x, uint32_t y, uint32_t x1, uint32_t y1, uint32_t colour)
  403. {
  404. printf("%s:%s(%d, %d, %d, %d, %d) @ %d\n", __FILE__, __func__,
  405. x, y, x1, y1, colour,
  406. __LINE__);
  407. drawLine(&mainWindow, x, y, x1, y1, getColour(colour));
  408. return 0;
  409. }
  410. int graphics_blit(long x, long y, long w, long h)
  411. {
  412. /* Just pool for events, no graphic thing to be done ATM */
  413. glfwPollEvents();
  414. return 0;
  415. }
  416. int getKeyStatus(int key)
  417. {
  418. return mainWindow.keyArray[key].curState;
  419. }
  420. /* Sync with 60Hz (or try to) */
  421. void vsync(void)
  422. {
  423. updateScreen(&mainWindow);
  424. }