aboutsummaryrefslogtreecommitdiff
path: root/main.c
blob: 91747659d32285a0cba2179c209143606c051444 (plain)
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#include <stdio.h>
#include <SDL2/SDL.h>

SDL_Texture* imv_load_image(SDL_Renderer *r, const char* path);

double clamp(double v, double min, double max) {
  if(v < min)
    return min;
  if(v > max)
    return max;
  return v;
}

int main(int argc, char** argv)
{
  if(argc < 2) {
    fprintf(stderr, "Usage: %s <image_file> <...>\n", argv[0]);
    exit(1);
  }

  if(SDL_Init(SDL_INIT_VIDEO) != 0) {
    fprintf(stderr, "SDL Failed to Init: %s\n", SDL_GetError());
    exit(1);
  }

  const int width = 1280;
  const int height = 720;

  SDL_Window *window = SDL_CreateWindow(
        "imv",
        SDL_WINDOWPOS_CENTERED,
        SDL_WINDOWPOS_CENTERED,
        width, height,
        SDL_WINDOW_RESIZABLE);

  SDL_Renderer *renderer =
    SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);

  const char** paths = (const char**)&argv[1];
  const int num_paths = argc - 1;
  int cur_path = -1;
  int next_path = 0;
  SDL_Texture *img = NULL;

  struct {
    double scale;
    int x, y;
  } view = {1,0,0};
  int quit = 0;
  int redraw = 1;
  while(!quit) {

    SDL_Event e;
    while(!quit && SDL_PollEvent(&e)) {
      switch(e.type) {
        case SDL_QUIT:
          quit = 1;
          break;
        case SDL_KEYDOWN:
          switch (e.key.keysym.sym) {
            case SDLK_q:
              quit = 1;
              break;
            case SDLK_RIGHT:
              next_path = (next_path + 1) % num_paths;
              break;
            case SDLK_LEFT:
              next_path -= 1;
              if(next_path < 0)
                next_path = num_paths - 1;
              break;
            case SDLK_UP:
              view.scale = clamp(view.scale + 0.1, 1, 10);
              redraw = 1;
              break;
            case SDLK_DOWN:
              view.scale = clamp(view.scale - 0.1, 1, 10);
              redraw = 1;
              break;
          }
          break;
        case SDL_MOUSEWHEEL:
          view.scale = clamp(view.scale + (0.1 * e.wheel.y), 1, 10);
          redraw = 1;
          break;
        case SDL_MOUSEMOTION:
          if(e.motion.state & SDL_BUTTON_LMASK) {
            view.x += e.motion.xrel;
            view.y += e.motion.yrel;
            redraw = 1;
          }
          break;
        case SDL_WINDOWEVENT:
          redraw = 1;
          break;
      }
    }

    if(quit) {
      break;
    }

    if(next_path != cur_path) {
      cur_path = next_path;
      fprintf(stdout, "current image: %s\n", paths[cur_path]);
      if(img) {
        SDL_DestroyTexture(img);
        img = NULL;
      }
      img = imv_load_image(renderer, paths[cur_path]);
      view.scale = 1;
      view.x = 0;
      view.y = 0;
      redraw = 1;
    }

    if(redraw) {
      SDL_RenderClear(renderer);

      if(img) {
        int img_w, img_h, img_access;
        unsigned int img_format;
        SDL_QueryTexture(img, &img_format, &img_access, &img_w, &img_h);
        SDL_Rect view_area = {
          view.x,
          view.y,
          img_w * view.scale,
          img_h * view.scale
        };
        SDL_RenderCopy(renderer, img, NULL, &view_area);
      }

      SDL_RenderPresent(renderer);
      redraw = 0;
    }
    SDL_Delay(10);
  }

  if(img) {
    SDL_DestroyTexture(img);
    img = NULL;
  }
  SDL_DestroyRenderer(renderer);
  SDL_DestroyWindow(window);
  SDL_Quit();

  return 0;
}