graphics.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  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. #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. void initDisplay(GLWindow *g)
  135. {
  136. int h = g->HEIGHT;
  137. int w = g->WIDTH;
  138. /// Initialize GLFW
  139. glfwInit();
  140. // Open screen OpenGL window
  141. if( !(g->windows=glfwCreateWindow( g->WIDTH, g->HEIGHT, "Main", NULL, NULL)) )
  142. {
  143. glfwTerminate();
  144. fprintf(stderr, "Window creation error...\n");
  145. return;
  146. }
  147. glfwMakeContextCurrent(g->windows);
  148. setupGL(g, g->WIDTH, g->HEIGHT);
  149. glfwSwapInterval(0); // Disable VSYNC
  150. glfwGetWindowSize(g->windows, &w, &h);
  151. glfwSetWindowUserPointer(g->windows, g->keyArray);
  152. glfwSetKeyCallback(g->windows, kbHandler);
  153. glfwSetWindowSizeCallback(g->windows, sizeHandler);
  154. }
  155. void drawPixel(GLWindow *gw, int x, int y, uint32_t colour)
  156. {
  157. uint8_t r,g,b,a;
  158. uint32_t offset = (y*gw->WIDTH*4)+4*x;
  159. if ((x < 0) || (x > gw->WIDTH) || (y < 0) || (y > gw->HEIGHT))
  160. return;
  161. b = colour & 0xFF;
  162. g = (colour >> 8) & 0xFF;
  163. r = (colour >> 16) & 0xFF;
  164. a = (colour >> 24) & 0xFF;
  165. gw->videoMemory[offset + 0] = a;
  166. gw->videoMemory[offset + 1] = r;
  167. gw->videoMemory[offset + 2] = g;
  168. gw->videoMemory[offset + 3] = b;
  169. }
  170. void drawLine(GLWindow *g, int x0, int y0, int x1, int y1, uint32_t colour)
  171. {
  172. int d, dx, dy, aincr, bincr, xincr, yincr, x, y;
  173. if (abs(x1 - x0) < abs(y1 - y0))
  174. {
  175. /* parcours par l'axe vertical */
  176. if (y0 > y1)
  177. {
  178. drawLine(g, x1, y1, x0, y0, colour);
  179. goto exit;
  180. }
  181. xincr = x1 > x0 ? 1 : -1;
  182. dy = y1 - y0;
  183. dx = abs(x1 - x0);
  184. d = 2 * dx - dy;
  185. aincr = 2 * (dx - dy);
  186. bincr = 2 * dx;
  187. x = x0;
  188. y = y0;
  189. drawPixel(g, x, y, colour);
  190. for (y = y0+1; y <= y1; y++)
  191. {
  192. if (d >= 0)
  193. {
  194. x += xincr;
  195. d += aincr;
  196. }
  197. else
  198. {
  199. d += bincr;
  200. }
  201. drawPixel(g, x, y, colour);
  202. }
  203. }
  204. else
  205. {
  206. /* parcours par l'axe horizontal */
  207. if (x0 > x1)
  208. {
  209. drawLine(g, x1, y1, x0, y0, colour);
  210. goto exit;
  211. }
  212. yincr = y1 > y0 ? 1 : -1;
  213. dx = x1 - x0;
  214. dy = abs(y1 - y0);
  215. d = 2 * dy - dx;
  216. aincr = 2 * (dy - dx);
  217. bincr = 2 * dy;
  218. x = x0;
  219. y = y0;
  220. drawPixel(g, x, y, colour);
  221. for (x = x0+1; x <= x1; ++x)
  222. {
  223. if (d >= 0)
  224. {
  225. y += yincr;
  226. d += aincr;
  227. }
  228. else
  229. {
  230. d += bincr;
  231. }
  232. drawPixel(g, x, y, colour);
  233. }
  234. }
  235. exit:
  236. return;
  237. }
  238. void drawCircle(GLWindow *g, int xc, int yc, int radius, uint32_t colour)
  239. {
  240. int f = 1 - radius;
  241. int ddF_x = 0;
  242. int ddF_y = -2 * radius;
  243. int x = 0;
  244. int y = radius;
  245. int pX, pY;
  246. pX = xc; pY = yc + radius;
  247. drawPixel(g, pX, pY, colour);
  248. pY -= (2*radius);
  249. drawPixel(g, pX, pY, colour);
  250. pY += radius; pX += radius;
  251. drawPixel(g, pX, pY, colour);
  252. pX -= (2*radius);
  253. drawPixel(g, pX, pY, colour);
  254. while(x < y)
  255. {
  256. if(f >= 0)
  257. {
  258. y--;
  259. ddF_y += 2;
  260. f += ddF_y;
  261. }
  262. x++;
  263. ddF_x += 2;
  264. f += ddF_x + 1;
  265. pX = xc+x ; pY = yc+y;
  266. drawPixel(g, pX, pY, colour);
  267. pX = xc-x ; pY = yc+y;
  268. drawPixel(g, pX, pY, colour);
  269. pX = xc+x ; pY = yc-y;
  270. drawPixel(g, pX, pY, colour);
  271. pX = xc-x ; pY = yc-y;
  272. drawPixel(g, pX, pY, colour);
  273. pX = xc+y ; pY = yc+x;
  274. drawPixel(g, pX, pY, colour);
  275. pX = xc-y ; pY = yc+x;
  276. drawPixel(g, pX, pY, colour);
  277. pX = xc+y ; pY = yc-x;
  278. drawPixel(g, pX, pY, colour);
  279. pX = xc-y ; pY = yc-x;
  280. drawPixel(g, pX, pY, colour);
  281. }
  282. return;
  283. }
  284. void drawRect(GLWindow *g, int x0, int y0, int w, int h, uint32_t colour)
  285. {
  286. drawLine(g, x0 , y0 , x0 + w, y0 , colour);
  287. drawLine(g, x0 + w, y0 , x0 + w, y0 + h, colour);
  288. drawLine(g, x0 + w, y0 + h, x0 , y0 + h, colour);
  289. drawLine(g, x0 , y0 + h, x0 , y0 , colour);
  290. }
  291. void drawFillrect(GLWindow *g, int x0, int y0, int w, int h, uint32_t colour)
  292. {
  293. int i;
  294. for(i = 0; i < h; i++)
  295. drawLine(g, x0, y0+i, x0+w, y0+i, colour);
  296. }
  297. void clearScreen(GLWindow *g)
  298. {
  299. memset(g->videoMemory, 0, sizeof(uint8_t) * g->WIDTH * g->HEIGHT * 4);
  300. }
  301. void updateScreen(GLWindow *g)
  302. {
  303. /*Update windows code */
  304. glfwMakeContextCurrent(g->windows);
  305. ShowScreen(g, g->WIDTH, g->HEIGHT);
  306. glfwSwapBuffers(g->windows);
  307. glfwPollEvents();
  308. }
  309. void updateScreenAndWait(GLWindow *g)
  310. {
  311. while (glfwGetKey(g->windows,GLFW_KEY_ESCAPE) != GLFW_PRESS)
  312. {
  313. updateScreen(g);
  314. glfwPollEvents();
  315. }
  316. while(glfwGetKey(g->windows,GLFW_KEY_ESCAPE) != GLFW_RELEASE)
  317. {
  318. glfwPollEvents();
  319. }
  320. }
  321. GLWindow mainWindow;
  322. int graphics_init()
  323. {
  324. GLWindowInitEx(&mainWindow, 256, 240);
  325. initDisplay(&mainWindow);
  326. clearScreen(&mainWindow);
  327. updateScreen(&mainWindow);
  328. return 0;
  329. }
  330. static unsigned long getColour(long color)
  331. {
  332. Palette *pal = &basicPalette[color];
  333. uint8_t r, g, b, a;
  334. r = pal->r;
  335. b = pal->b;
  336. g = pal->g;
  337. a = 255;//pal->a;
  338. return (b << 24) | (g << 16) | (r << 8) | a;
  339. }
  340. int graphics_drawpixel(long x, long y, long color)
  341. {
  342. drawPixel(&mainWindow, x, y, getColour(color));
  343. return 0;
  344. }
  345. int graphics_drawline(long x, long y, long x1, long y1, long color)
  346. {
  347. drawLine(&mainWindow, x, y, x1, y1, getColour(color));
  348. return 0;
  349. }
  350. int graphics_blit(long x, long y, long w, long h)
  351. {
  352. updateScreen(&mainWindow);
  353. return 0;
  354. }
  355. int getKeyStatus(int key)
  356. {
  357. return mainWindow.keyArray[key].curState;
  358. }