graphics.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  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. }
  128. void sizeHandler(GLFWwindow *window, int xs, int ys)
  129. {
  130. glfwMakeContextCurrent(window);
  131. glViewport(0, 0, xs, ys);
  132. }
  133. static void error_callback(int error, const char *description)
  134. {
  135. puts(description);
  136. }
  137. void initDisplay(GLWindow *g)
  138. {
  139. int h = g->HEIGHT;
  140. int w = g->WIDTH;
  141. /// Initialize GLFW
  142. glfwInit();
  143. glfwSetErrorCallback(error_callback);
  144. // Open screen OpenGL window
  145. if (!(g->windows = glfwCreateWindow(g->WIDTH, g->HEIGHT, "Main", NULL, NULL)))
  146. {
  147. glfwTerminate();
  148. fprintf(stderr, "Window creation error...\n");
  149. abort();
  150. }
  151. glfwSetWindowAspectRatio(g->windows, 4, 3);
  152. glfwMakeContextCurrent(g->windows);
  153. setupGL(g, g->WIDTH, g->HEIGHT);
  154. glfwSwapInterval(1); // We need vsync
  155. glfwGetWindowSize(g->windows, &w, &h);
  156. glfwSetWindowUserPointer(g->windows, g->keyArray);
  157. glfwSetKeyCallback(g->windows, kbHandler);
  158. glfwSetWindowSizeCallback(g->windows, sizeHandler);
  159. }
  160. void drawPixel(GLWindow *gw, int x, int y, uint32_t colour)
  161. {
  162. uint8_t r, g, b, a;
  163. uint32_t offset = (y * gw->WIDTH * 4U) + 4U * x;
  164. if ((x < 0) || (x > gw->WIDTH) || (y < 0) || (y > gw->HEIGHT))
  165. {
  166. return;
  167. }
  168. b = colour & 0xFF;
  169. g = (colour >> 8) & 0xFF;
  170. r = (colour >> 16) & 0xFF;
  171. a = (colour >> 24) & 0xFF;
  172. gw->videoMemory[offset + 0] = a;
  173. gw->videoMemory[offset + 1] = r;
  174. gw->videoMemory[offset + 2] = g;
  175. gw->videoMemory[offset + 3] = b;
  176. }
  177. void drawLine(GLWindow *g, int x0, int y0, int x1, int y1, uint32_t colour)
  178. {
  179. int d, dx, dy, aincr, bincr, xincr, yincr, x, y;
  180. if (abs(x1 - x0) < abs(y1 - y0))
  181. {
  182. /* parcours par l'axe vertical */
  183. if (y0 > y1)
  184. {
  185. drawLine(g, x1, y1, x0, y0, colour);
  186. goto exit;
  187. }
  188. xincr = x1 > x0 ? 1 : -1;
  189. dy = y1 - y0;
  190. dx = abs(x1 - x0);
  191. d = 2 * dx - dy;
  192. aincr = 2 * (dx - dy);
  193. bincr = 2 * dx;
  194. x = x0;
  195. y = y0;
  196. drawPixel(g, x, y, colour);
  197. for (y = y0 + 1 ; y <= y1 ; y++)
  198. {
  199. if (d >= 0)
  200. {
  201. x += xincr;
  202. d += aincr;
  203. }
  204. else
  205. {
  206. d += bincr;
  207. }
  208. drawPixel(g, x, y, colour);
  209. }
  210. }
  211. else
  212. {
  213. /* parcours par l'axe horizontal */
  214. if (x0 > x1)
  215. {
  216. drawLine(g, x1, y1, x0, y0, colour);
  217. goto exit;
  218. }
  219. yincr = y1 > y0 ? 1 : -1;
  220. dx = x1 - x0;
  221. dy = abs(y1 - y0);
  222. d = 2 * dy - dx;
  223. aincr = 2 * (dy - dx);
  224. bincr = 2 * dy;
  225. x = x0;
  226. y = y0;
  227. drawPixel(g, x, y, colour);
  228. for (x = x0 + 1 ; x <= x1 ; ++x)
  229. {
  230. if (d >= 0)
  231. {
  232. y += yincr;
  233. d += aincr;
  234. }
  235. else
  236. {
  237. d += bincr;
  238. }
  239. drawPixel(g, x, y, colour);
  240. }
  241. }
  242. exit:
  243. return;
  244. }
  245. void drawCircle(GLWindow *g, int xc, int yc, int radius, uint32_t colour)
  246. {
  247. int f = 1 - radius;
  248. int ddF_x = 0;
  249. int ddF_y = -2 * radius;
  250. int x = 0;
  251. int y = radius;
  252. int pX, pY;
  253. pX = xc;
  254. pY = yc + radius;
  255. drawPixel(g, pX, pY, colour);
  256. pY -= (2 * radius);
  257. drawPixel(g, pX, pY, colour);
  258. pY += radius;
  259. pX += radius;
  260. drawPixel(g, pX, pY, colour);
  261. pX -= (2 * radius);
  262. drawPixel(g, pX, pY, colour);
  263. while (x < y)
  264. {
  265. if (f >= 0)
  266. {
  267. y--;
  268. ddF_y += 2;
  269. f += ddF_y;
  270. }
  271. x++;
  272. ddF_x += 2;
  273. f += ddF_x + 1;
  274. pX = xc + x;
  275. pY = yc + y;
  276. drawPixel(g, pX, pY, colour);
  277. pX = xc - x;
  278. pY = yc + y;
  279. drawPixel(g, pX, pY, colour);
  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 + y;
  287. pY = yc + x;
  288. drawPixel(g, pX, pY, colour);
  289. pX = xc - y;
  290. pY = yc + x;
  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. }
  299. }
  300. void drawRect(GLWindow *g, int x0, int y0, int w, int h, uint32_t colour)
  301. {
  302. drawLine(g, x0, y0, x0 + w, y0, colour);
  303. drawLine(g, x0 + w, y0, x0 + w, y0 + h, colour);
  304. drawLine(g, x0 + w, y0 + h, x0, y0 + h, colour);
  305. drawLine(g, x0, y0 + h, x0, y0, colour);
  306. }
  307. void drawFillrect(GLWindow *g, int x0, int y0, int w, int h, uint32_t colour)
  308. {
  309. int i;
  310. for (i = 0 ; i < h ; i++)
  311. {
  312. drawLine(g, x0, y0 + i, x0 + w, y0 + i, colour);
  313. }
  314. }
  315. void clearScreen(GLWindow *g)
  316. {
  317. memset(g->videoMemory, 0, sizeof(uint8_t) * g->WIDTH * g->HEIGHT * 4);
  318. }
  319. void updateScreen(GLWindow *g)
  320. {
  321. /*Update windows code */
  322. glfwMakeContextCurrent(g->windows);
  323. ShowScreen(g, g->WIDTH, g->HEIGHT);
  324. glfwSwapBuffers(g->windows);
  325. glfwPollEvents();
  326. }
  327. void updateScreenAndWait(GLWindow *g)
  328. {
  329. while (glfwGetKey(g->windows, GLFW_KEY_ESCAPE) != GLFW_PRESS)
  330. {
  331. updateScreen(g);
  332. glfwPollEvents();
  333. }
  334. while (glfwGetKey(g->windows, GLFW_KEY_ESCAPE) != GLFW_RELEASE)
  335. {
  336. glfwPollEvents();
  337. }
  338. }
  339. GLWindow mainWindow;
  340. int graphics_init()
  341. {
  342. GLWindowInitEx(&mainWindow, 256, 240);
  343. initDisplay(&mainWindow);
  344. clearScreen(&mainWindow);
  345. updateScreen(&mainWindow);
  346. return 0;
  347. }
  348. static uint32_t getColour(long color)
  349. {
  350. Palette *pal = &basicPalette[color];
  351. uint8_t r, g, b, a;
  352. r = pal->r;
  353. b = pal->b;
  354. g = pal->g;
  355. a = 255;//pal->a;
  356. return (b << 24) | (g << 16) | (r << 8) | a;
  357. }
  358. int graphics_drawpixel(long x, long y, long color)
  359. {
  360. drawPixel(&mainWindow, x, y, getColour(color));
  361. return 0;
  362. }
  363. int graphics_drawline(long x, long y, long x1, long y1, long color)
  364. {
  365. drawLine(&mainWindow, x, y, x1, y1, getColour(color));
  366. return 0;
  367. }
  368. int graphics_blit(long x, long y, long w, long h)
  369. {
  370. /* Just pool for events, no graphic thing to be done ATM */
  371. glfwPollEvents();
  372. return 0;
  373. }
  374. int getKeyStatus(int key)
  375. {
  376. return mainWindow.keyArray[key].curState;
  377. }
  378. /* Sync with 60Hz (or try to) */
  379. void vsync(void)
  380. {
  381. updateScreen(&mainWindow);
  382. }