graphics.c 9.7 KB

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