riCOM_cpp
This repository contains the C++ implementation of the riCOM (Real Time Centre Of Mass) algorithm for 4D Scanning electron microscopy.
SdlImageWindow.cpp
Go to the documentation of this file.
1 /* Copyright (C) 2021 Thomas Friedrich, Chu-Ping Yu,
2  * University of Antwerp - All Rights Reserved.
3  * You may use, distribute and modify
4  * this code under the terms of the GPL3 license.
5  * You should have received a copy of the GPL3 license with
6  * this file. If not, please visit:
7  * https://www.gnu.org/licenses/gpl-3.0.en.html
8  *
9  * Authors:
10  * Thomas Friedrich <thomas.friedrich@uantwerpen.be>
11  * Chu-Ping Yu <chu-ping.yu@uantwerpen.be>
12  */
13 
14 #include "SdlImageWindow.h"
15 
16 SdlImageWindow::SdlImageWindow(const char *title, SDL_Surface *srf, int nx, int ny, float scale)
17 {
18  this->title = title;
19  create_window(srf, nx, ny, scale);
20 };
21 
23 {
24  this->title = title;
25  this->window = NULL; // Pointer for the window
26  this->renderer = NULL; // Pointer for the renderer
27  this->tex = NULL; // Texture for the window
28  this->srf = NULL; // Surface (data source)
29 };
30 
31 void SdlImageWindow::create_window(SDL_Surface *srf, int nx, int ny, float scale)
32 {
33  // Creating window
34  window = SDL_CreateWindow(title, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, scale * nx, scale * ny, SDL_WINDOW_SHOWN | SDL_WINDOW_RESIZABLE | SDL_WINDOW_OPENGL);
35  if (window == NULL)
36  {
37  std::cout << "Window could not be created! SDL Error: " << SDL_GetError() << std::endl;
38  }
39  // Creating Renderer
40  renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
41  if (renderer == NULL)
42  {
43  std::cout << "Renderer could not be created! SDL Error: " << SDL_GetError() << std::endl;
44  }
45  // Creating texture for hardware rendering
46  tex = SDL_CreateTexture(renderer, SDL_GetWindowPixelFormat(window), SDL_TEXTUREACCESS_STATIC, nx, ny);
47  if (tex == NULL)
48  {
49  std::cout << "Texture could not be created! SDL Error: " << SDL_GetError() << std::endl;
50  }
51  // Maintain Pixel aspect ratio on resizing
52  if (SDL_RenderSetLogicalSize(renderer, scale * nx, scale * ny) < 0)
53  {
54  std::cout << "Logical size could not be set! SDL Error: " << SDL_GetError() << std::endl;
55  }
56  this->srf = srf;
57 }
58 
60 {
61  if (srf != NULL)
62  {
63  if (SDL_UpdateTexture(tex, NULL, srf->pixels, srf->pitch) == -1)
64  {
65  std::cout << "SDL_UpdateTexture failed: " << SDL_GetError() << std::endl;
66  }
67  if (SDL_RenderCopy(renderer, tex, NULL, NULL) == -1)
68  {
69  std::cout << "SDL_RenderCopy failed: " << SDL_GetError() << std::endl;
70  }
71  SDL_RenderPresent(renderer);
72  }
73 }
const char * title
void create_window(SDL_Surface *srf, int nx, int ny, float scale)
SdlImageWindow(const char *title)