graphics.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  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. void drawPixel(GLWindow *gw, int x, int 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 < 0) || (x > gw->WIDTH) || (y < 0) || (y > 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. void drawLine(GLWindow *g, int x0, int y0, int x1, int y1, uint32_t colour)
  180. {
  181. int d, dx, dy, aincr, bincr, xincr, yincr, x, y;
  182. if (abs(x1 - x0) < abs(y1 - y0))
  183. {
  184. /* parcours par l'axe vertical */
  185. if (y0 > y1)
  186. {
  187. drawLine(g, x1, y1, x0, y0, colour);
  188. goto exit;
  189. }
  190. xincr = x1 > x0 ? 1 : -1;
  191. dy = y1 - y0;
  192. dx = abs(x1 - x0);
  193. d = 2 * dx - dy;
  194. aincr = 2 * (dx - dy);
  195. bincr = 2 * dx;
  196. x = x0;
  197. y = y0;
  198. drawPixel(g, x, y, colour);
  199. for (y = y0 + 1 ; y <= y1 ; y++)
  200. {
  201. if (d >= 0)
  202. {
  203. x += xincr;
  204. d += aincr;
  205. }
  206. else
  207. {
  208. d += bincr;
  209. }
  210. drawPixel(g, x, y, colour);
  211. }
  212. }
  213. else
  214. {
  215. /* parcours par l'axe horizontal */
  216. if (x0 > x1)
  217. {
  218. drawLine(g, x1, y1, x0, y0, colour);
  219. goto exit;
  220. }
  221. yincr = y1 > y0 ? 1 : -1;
  222. dx = x1 - x0;
  223. dy = abs(y1 - y0);
  224. d = 2 * dy - dx;
  225. aincr = 2 * (dy - dx);
  226. bincr = 2 * dy;
  227. x = x0;
  228. y = y0;
  229. drawPixel(g, x, y, colour);
  230. for (x = x0 + 1 ; x <= x1 ; ++x)
  231. {
  232. if (d >= 0)
  233. {
  234. y += yincr;
  235. d += aincr;
  236. }
  237. else
  238. {
  239. d += bincr;
  240. }
  241. drawPixel(g, x, y, colour);
  242. }
  243. }
  244. exit:
  245. return;
  246. }
  247. void drawCircle(GLWindow *g, int xc, int yc, int radius, uint32_t colour)
  248. {
  249. int f = 1 - radius;
  250. int ddF_x = 0;
  251. int ddF_y = -2 * radius;
  252. int x = 0;
  253. int y = radius;
  254. int pX, pY;
  255. pX = xc;
  256. pY = yc + radius;
  257. drawPixel(g, pX, pY, colour);
  258. pY -= (2 * radius);
  259. drawPixel(g, pX, pY, colour);
  260. pY += radius;
  261. pX += radius;
  262. drawPixel(g, pX, pY, colour);
  263. pX -= (2 * radius);
  264. drawPixel(g, pX, pY, colour);
  265. while (x < y)
  266. {
  267. if (f >= 0)
  268. {
  269. y--;
  270. ddF_y += 2;
  271. f += ddF_y;
  272. }
  273. x++;
  274. ddF_x += 2;
  275. f += ddF_x + 1;
  276. pX = xc + x;
  277. pY = yc + y;
  278. drawPixel(g, pX, pY, colour);
  279. pX = xc - x;
  280. pY = yc + y;
  281. drawPixel(g, pX, pY, colour);
  282. pX = xc + x;
  283. pY = yc - y;
  284. drawPixel(g, pX, pY, colour);
  285. pX = xc - x;
  286. pY = yc - y;
  287. drawPixel(g, pX, pY, colour);
  288. pX = xc + y;
  289. pY = yc + x;
  290. drawPixel(g, pX, pY, colour);
  291. pX = xc - y;
  292. pY = yc + x;
  293. drawPixel(g, pX, pY, colour);
  294. pX = xc + y;
  295. pY = yc - x;
  296. drawPixel(g, pX, pY, colour);
  297. pX = xc - y;
  298. pY = yc - x;
  299. drawPixel(g, pX, pY, colour);
  300. }
  301. }
  302. void drawRect(GLWindow *g, int x0, int y0, int w, int h, uint32_t colour)
  303. {
  304. drawLine(g, x0, y0, x0 + w, y0, colour);
  305. drawLine(g, x0 + w, y0, x0 + w, y0 + h, colour);
  306. drawLine(g, x0 + w, y0 + h, x0, y0 + h, colour);
  307. drawLine(g, x0, y0 + h, x0, y0, colour);
  308. }
  309. void drawFillrect(GLWindow *g, int x0, int y0, int w, int h, uint32_t colour)
  310. {
  311. int i;
  312. for (i = 0 ; i < h ; i++)
  313. {
  314. drawLine(g, x0, y0 + i, x0 + w, y0 + i, colour);
  315. }
  316. }
  317. void clearScreen(GLWindow *g)
  318. {
  319. memset(g->videoMemory, 0, sizeof(uint8_t) * g->WIDTH * g->HEIGHT * 4);
  320. }
  321. void updateScreen(GLWindow *g)
  322. {
  323. /*Update windows code */
  324. glfwMakeContextCurrent(g->windows);
  325. ShowScreen(g, g->WIDTH, g->HEIGHT);
  326. glfwSwapBuffers(g->windows);
  327. glfwPollEvents();
  328. }
  329. void updateScreenAndWait(GLWindow *g)
  330. {
  331. while (glfwGetKey(g->windows, GLFW_KEY_ESCAPE) != GLFW_PRESS)
  332. {
  333. updateScreen(g);
  334. glfwPollEvents();
  335. }
  336. while (glfwGetKey(g->windows, GLFW_KEY_ESCAPE) != GLFW_RELEASE)
  337. {
  338. glfwPollEvents();
  339. }
  340. }
  341. GLWindow mainWindow;
  342. int graphics_init()
  343. {
  344. GLWindowInitEx(&mainWindow, 256, 240);
  345. initDisplay(&mainWindow);
  346. clearScreen(&mainWindow);
  347. updateScreen(&mainWindow);
  348. return 0;
  349. }
  350. static uint32_t getColour(long color)
  351. {
  352. Palette *pal = &basicPalette[color];
  353. uint8_t r, g, b, a;
  354. r = pal->r << 2;
  355. b = pal->b << 2;
  356. g = pal->g << 2;
  357. a = 255;//pal->a;
  358. return (b << 24) | (g << 16) | (r << 8) | a;
  359. }
  360. int graphics_drawpixel(long x, long y, long color)
  361. {
  362. drawPixel(&mainWindow, x, y, getColour(color));
  363. return 0;
  364. }
  365. int graphics_drawline(long x, long y, long x1, long y1, long color)
  366. {
  367. drawLine(&mainWindow, x, y, x1, y1, getColour(color));
  368. return 0;
  369. }
  370. int graphics_blit(long x, long y, long w, long h)
  371. {
  372. /* Just pool for events, no graphic thing to be done ATM */
  373. glfwPollEvents();
  374. return 0;
  375. }
  376. int getKeyStatus(int key)
  377. {
  378. return mainWindow.keyArray[key].curState;
  379. }
  380. /* Sync with 60Hz (or try to) */
  381. void vsync(void)
  382. {
  383. updateScreen(&mainWindow);
  384. }