plotWindow.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /*
  2. 2D FDTD simulator
  3. Copyright (C) 2019 Emilia Blåsten
  4. This program is free software: you can redistribute it and/or
  5. modify it under the terms of the GNU Affero General Public License
  6. as published by the Free Software Foundation, either version 3 of
  7. the License, or (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Affero General Public License for more details.
  12. You should have received a copy of the GNU Affero General Public
  13. License along with this program. If not, see
  14. <http://www.gnu.org/licenses/>.
  15. */
  16. /* plotWindow.c: plotWindow initialization and destruction (of all of
  17. * them at once). */
  18. #include "plotWindow.h"
  19. #include <stdio.h>
  20. #include <stdlib.h>
  21. // A window is self-explanatory in a graphical desktop
  22. // The renderer is needed for each window where we draw. It uses
  23. // hardware acceleration behind the scenes and we can use it to
  24. // blit textures, draw lines, points, sprites, etc...
  25. // A texture is used if the application draws each pixel and then "blits"
  26. // The buffer containing all pixel colors will be inside a SDL_Surface
  27. plotWindow* createPlotWindow(int x, int y, int width, int height, char *title)
  28. {
  29. plotWindow *plotter;
  30. plotter = (plotWindow *)calloc(1, sizeof(plotWindow));
  31. if(!plotter) {
  32. perror("plotWindowInit");
  33. fprintf(stderr, "Could not allocate memory for plotWindow sturct.\n");
  34. exit(-1);
  35. }
  36. plotter->title = title;
  37. plotter->width = width;
  38. plotter->height = height;
  39. plotter->minf = -2;
  40. plotter->maxf = 2;
  41. plotter->red1 = 255;
  42. plotter->green1 = 0;
  43. plotter->blue1 = 0;
  44. plotter->red2 = 0;
  45. plotter->green2 = 0;
  46. plotter->blue2 = 255;
  47. // initialize SDL
  48. if( SDL_Init(SDL_INIT_VIDEO) < 0) {
  49. perror("SDL_INIT");
  50. fprintf(stderr, "SDL initialization error: %s\n", SDL_GetError());
  51. exit(-1);
  52. }
  53. plotter->window = SDL_CreateWindow(title, x, y, width, height,
  54. SDL_WINDOW_SHOWN);
  55. plotter->renderer = SDL_CreateRenderer(plotter->window, -1, 0);
  56. plotter->texture = SDL_CreateTexture(plotter->renderer,
  57. SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STREAMING,
  58. width, height);
  59. plotter->surface = SDL_CreateRGBSurface(0, width, height, 32,
  60. 0x00FF0000,
  61. 0x0000FF00,
  62. 0x000000FF,
  63. 0xFF000000);
  64. if(!plotter->window || !plotter->renderer || !plotter->texture
  65. || !plotter->surface) {
  66. perror("SDL_initialization");
  67. fprintf(stderr, "SDL elements initialization error: %s\n", SDL_GetError());
  68. exit(-1);
  69. }
  70. // Causes problems with multiple windows on top of each other
  71. // SDL_ShowCursor(0);
  72. return plotter;
  73. }
  74. int wantToStopPlot()
  75. {
  76. SDL_Event event;
  77. while(SDL_PollEvent(&event)) {
  78. switch (event.type) {
  79. case SDL_WINDOWEVENT:
  80. if(event.window.event == SDL_WINDOWEVENT_CLOSE)
  81. return 1;
  82. break;
  83. case SDL_QUIT:
  84. case SDL_KEYDOWN:
  85. return 1;
  86. }
  87. }
  88. return 0;
  89. }
  90. void quitAllPlotting() {
  91. SDL_Quit();
  92. return;
  93. }
  94. void destroyPlotWindow(plotWindow *plotter) {
  95. SDL_FreeSurface(plotter->surface);
  96. SDL_DestroyTexture(plotter->texture);
  97. SDL_DestroyRenderer(plotter->renderer);
  98. SDL_DestroyWindow(plotter->window);
  99. free(plotter);
  100. return;
  101. }