graphics.c 9.6 KB

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