graphics.c 9.7 KB

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