graphics.c 9.8 KB

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