graphics.c 9.8 KB

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