graphics.c 9.6 KB

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