~ruther/CTU-FEE-B0B35APO-Semestral-project

ref: 762b2cf9fffa99add8e2a82a286b9cfe189c15e8 CTU-FEE-B0B35APO-Semestral-project/image-viewer/src/xwin_sdl.c -rw-r--r-- 1.8 KiB
762b2cf9 — František Boháček feat: add zoom reset 3 years ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/*
 * File name: xwin_sdl.c
 * Date:      2015/06/18 14:37
 * Author:    Jan Faigl
 */

#include <assert.h>

#include <SDL.h>
#include <SDL_image.h>

#include "SDL_surface.h"
#include "SDL_video.h"
#include "xwin_sdl.h"

static SDL_Window *win = NULL;

int xwin_init(uint16_t w, uint16_t h) {
  int r = 0;
  r = SDL_Init(SDL_INIT_VIDEO);
  assert(win == NULL);
  win = SDL_CreateWindow("APO Test program", SDL_WINDOWPOS_UNDEFINED,
                         SDL_WINDOWPOS_UNDEFINED, w, h, SDL_WINDOW_SHOWN);
  assert(win != NULL);
  SDL_SetWindowTitle(win, "APO Test program");
  SDL_Surface *surface = SDL_CreateRGBSurface(
                         32, 32, 24, 32 * 3, 0xff, 0xff00, 0xff0000, 0x0000);
  SDL_SetWindowIcon(win, surface);
  SDL_FreeSurface(surface);

  SDL_SetWindowResizable(win, SDL_FALSE);
  return r;
}

void xwin_close() {
  assert(win != NULL);
  SDL_DestroyWindow(win);
  SDL_Quit();
}

void xwin_redraw(uint16_t w, uint16_t h, uint8_t *img) {
  assert(img && win);
  SDL_Surface *scr = SDL_GetWindowSurface(win);

  for (int y = 0; y < scr->h && y < h; ++y) {
    for (int x = 0; x < scr->w && x < w; ++x) {
      const int idx = (y * scr->w + x) * scr->format->BytesPerPixel;
      Uint8 *px = (Uint8 *)scr->pixels + idx;
      uint64_t position_in_img = (y * w + x) * 3;
      *(px + scr->format->Rshift / 8) = *(img + position_in_img);
      *(px + scr->format->Gshift / 8) = *(img + position_in_img + 1);
      *(px + scr->format->Bshift / 8) = *(img + position_in_img + 2);
    }
  }
  SDL_UpdateWindowSurface(win);
  SDL_FreeSurface(scr);
}

void xwin_save_image(char *output_name) {
  IMG_Init(IMG_INIT_PNG);
  SDL_Surface *scr = SDL_GetWindowSurface(win);
  IMG_SavePNG(scr, output_name);
  SDL_FreeSurface(scr);
  IMG_Quit();
}

bool xwin_poll_event(SDL_Event *event) {
  return SDL_PollEvent(event);
}

/* end of xwin_sdl.c */
Do not follow this link