aboutsummaryrefslogtreecommitdiff
path: root/src/texture.c
blob: a882320c6b31f4b0cef97551852b866c21190d98 (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
/* Copyright (c) 2015 Harry Jeffery

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
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 General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/

#include "texture.h"

void imv_init_texture(struct imv_texture *tex, SDL_Renderer *r)
{
  tex->renderer = r;
  tex->num_chunks = 0;
  tex->chunks = NULL;

  SDL_RendererInfo ri;
  SDL_GetRendererInfo(r, &ri);
  tex->chunk_width = ri.max_texture_width != 0 ? ri.max_texture_width : 4096;
  tex->chunk_height = ri.max_texture_height != 0 ? ri.max_texture_height : 4096;
}

void imv_destroy_texture(struct imv_texture *tex)
{
  if(tex->num_chunks > 0) {
    for(int i = 0; i < tex->num_chunks; ++i) {
      SDL_DestroyTexture(tex->chunks[i]);
    }
    free(tex->chunks);
    tex->num_chunks = 0;
    tex->chunks = NULL;
    tex->renderer = NULL;
  }
}

int imv_texture_set_image(struct imv_texture *tex, FIBITMAP *image)
{
  tex->width = FreeImage_GetWidth(image);
  tex->height = FreeImage_GetHeight(image);

  /* figure out how many chunks are needed, and create them */
  if(tex->num_chunks > 0) {
    for(int i = 0; i < tex->num_chunks; ++i) {
      SDL_DestroyTexture(tex->chunks[i]);
    }
    free(tex->chunks);
  }

  tex->num_chunks_wide = 1 + tex->width / tex->chunk_width;
  tex->num_chunks_tall = 1 + tex->height / tex->chunk_height;

  tex->last_chunk_width = tex->width % tex->chunk_width;
  tex->last_chunk_height = tex->height % tex->chunk_height;

  if(tex->last_chunk_width == 0) {
    tex->last_chunk_width = tex->chunk_width;
  }
  if(tex->last_chunk_height == 0) {
    tex->last_chunk_height = tex->chunk_height;
  }

  tex->num_chunks = tex->num_chunks_wide * tex->num_chunks_tall;
  tex->chunks = (SDL_Texture**)malloc(sizeof(SDL_Texture*) * tex->num_chunks);

  int failed_at = -1;
  for(int y = 0; y < tex->num_chunks_tall; ++y) {
    for(int x = 0; x < tex->num_chunks_wide; ++x) {
      const int is_last_h_chunk = (x == tex->num_chunks_wide - 1);
      const int is_last_v_chunk = (y == tex->num_chunks_tall - 1);
      tex->chunks[x + y * tex->num_chunks_wide] =
        SDL_CreateTexture(tex->renderer,
          SDL_PIXELFORMAT_ARGB8888,
          SDL_TEXTUREACCESS_STATIC,
          is_last_h_chunk ? tex->last_chunk_width : tex->chunk_width,
          is_last_v_chunk ? tex->last_chunk_height : tex->chunk_height);
      SDL_SetTextureBlendMode(tex->chunks[x + y * tex->num_chunks_wide],
          SDL_BLENDMODE_BLEND);
      if(tex->chunks[x + y * tex->num_chunks_wide] == NULL) {
        failed_at = x + y * tex->num_chunks_wide;
        break;
      }
    }
  }

  if(failed_at != -1) {
    for(int i = 0; i <= failed_at; ++i) {
      SDL_DestroyTexture(tex->chunks[i]);
    }
    free(tex->chunks);
    tex->num_chunks = 0;
    tex->chunks = NULL;
    return 1;
  }

  BYTE* pixels = (BYTE*)malloc(4 * tex->width * tex->height);
  FreeImage_ConvertToRawBits(pixels, image, 4 * tex->width, 32,
      FI_RGBA_RED_MASK, FI_RGBA_GREEN_MASK, FI_RGBA_BLUE_MASK, TRUE);

  for(int y = 0; y < tex->num_chunks_tall; ++y) {
    for(int x = 0; x < tex->num_chunks_wide; ++x) {
      ptrdiff_t offset = 4 * x * tex->chunk_width +
        y * 4 * tex->width * tex->chunk_height;
      BYTE* addr = pixels + offset;
      SDL_UpdateTexture(tex->chunks[x + y * tex->num_chunks_wide],
          NULL, addr, 4 * tex->width);
    }
  }

  free(pixels);
  return 0;
}

void imv_texture_draw(struct imv_texture *tex, int bx, int by, double scale)
{
  int offset_x = 0;
  int offset_y = 0;
  for(int y = 0; y < tex->num_chunks_tall; ++y) {
    for(int x = 0; x < tex->num_chunks_wide; ++x) {
      int img_w, img_h;
      SDL_QueryTexture(tex->chunks[x + y * tex->num_chunks_wide], NULL, NULL,
        &img_w, &img_h);
      SDL_Rect view_area = {
        bx + offset_x,
        by + offset_y,
        img_w * scale,
        img_h * scale
      };
      SDL_RenderCopy(tex->renderer,
          tex->chunks[x + y * tex->num_chunks_wide], NULL, &view_area);
      offset_x += tex->chunk_width * scale;
    }
    offset_x = 0;
    offset_y += tex->chunk_height * scale;
  }
}