123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129 |
- /*
- 2D FDTD simulator
- Copyright (C) 2019 Emilia Blåsten
- This program is free software: you can redistribute it and/or
- modify it under the terms of the GNU Affero General Public License
- as published by the Free Software Foundation, either version 3 of
- the License, or (at your option) any later version.
- This program is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- Affero General Public License for more details.
- You should have received a copy of the GNU Affero General Public
- License along with this program. If not, see
- <http://www.gnu.org/licenses/>.
- */
- /* plotWindow.c: plotWindow initialization and destruction (of all of
- * them at once). */
- #include "plotWindow.h"
- #include <stdio.h>
- #include <stdlib.h>
- // A window is self-explanatory in a graphical desktop
- // The renderer is needed for each window where we draw. It uses
- // hardware acceleration behind the scenes and we can use it to
- // blit textures, draw lines, points, sprites, etc...
- // A texture is used if the application draws each pixel and then "blits"
- // The buffer containing all pixel colors will be inside a SDL_Surface
- plotWindow* createPlotWindow(int x, int y, int width, int height, char *title)
- {
- plotWindow *plotter;
- plotter = (plotWindow *)calloc(1, sizeof(plotWindow));
- if(!plotter) {
- perror("plotWindowInit");
- fprintf(stderr, "Could not allocate memory for plotWindow sturct.\n");
- exit(-1);
- }
- plotter->title = title;
- plotter->width = width;
- plotter->height = height;
- plotter->minf = -2;
- plotter->maxf = 2;
- plotter->red1 = 255;
- plotter->green1 = 0;
- plotter->blue1 = 0;
- plotter->red2 = 0;
- plotter->green2 = 0;
- plotter->blue2 = 255;
- // initialize SDL
- if( SDL_Init(SDL_INIT_VIDEO) < 0) {
- perror("SDL_INIT");
- fprintf(stderr, "SDL initialization error: %s\n", SDL_GetError());
- exit(-1);
- }
- plotter->window = SDL_CreateWindow(title, x, y, width, height,
- SDL_WINDOW_SHOWN);
- plotter->renderer = SDL_CreateRenderer(plotter->window, -1, 0);
- plotter->texture = SDL_CreateTexture(plotter->renderer,
- SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STREAMING,
- width, height);
- plotter->surface = SDL_CreateRGBSurface(0, width, height, 32,
- 0x00FF0000,
- 0x0000FF00,
- 0x000000FF,
- 0xFF000000);
- if(!plotter->window || !plotter->renderer || !plotter->texture
- || !plotter->surface) {
- perror("SDL_initialization");
- fprintf(stderr, "SDL elements initialization error: %s\n", SDL_GetError());
- exit(-1);
- }
- // Causes problems with multiple windows on top of each other
- // SDL_ShowCursor(0);
- return plotter;
- }
- int wantToStopPlot()
- {
- SDL_Event event;
- while(SDL_PollEvent(&event)) {
- switch (event.type) {
- case SDL_WINDOWEVENT:
- if(event.window.event == SDL_WINDOWEVENT_CLOSE)
- return 1;
- break;
- case SDL_QUIT:
- case SDL_KEYDOWN:
- return 1;
- }
- }
- return 0;
- }
- void quitAllPlotting() {
- SDL_Quit();
- return;
- }
- void destroyPlotWindow(plotWindow *plotter) {
- SDL_FreeSurface(plotter->surface);
- SDL_DestroyTexture(plotter->texture);
- SDL_DestroyRenderer(plotter->renderer);
- SDL_DestroyWindow(plotter->window);
- free(plotter);
- return;
- }
|