summaryrefslogtreecommitdiff
path: root/src/client/image.c
blob: 52585e2b5bf718c705cd585d9bc71ed9b0654326 (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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>

#include "main.h"
#include "list.h"
#include "storage.h"

#include "interface.h"
#include "image.h"

static list_t *listStorage;

static bool_t isImageDataInit = FALSE;

bool_t
isImageInicialized()
{
   return isImageDataInit;
}

/*
 * Inicializuje globalny zoznam obrazkov
 */
void
initImageData()
{
   assert(isInterfaceInicialized() == TRUE);
#ifdef DEBUG
   printf(_("Initializing image database\n"));
#endif
   listStorage = newStorage();
   isImageDataInit = TRUE;
}

static SDL_Surface *
loadImage(const char *filename, int alpha)
{
   SDL_Surface *tmp;
   SDL_Surface *ret;

   char str[STR_PATH_SIZE];


   if (isFillPath(filename)) {
      strcpy(str, filename);
   }
   else {
      sprintf(str, PATH_IMAGE "%s", filename);
   }

   accessExistFile(str);

   if ((tmp = IMG_Load(str)) == NULL) {
      fprintf(stderr, "%s\n", SDL_GetError());
      return NULL;
   }

   if ((ret =
        (alpha) ? SDL_DisplayFormatAlpha(tmp) : SDL_DisplayFormat(tmp)) ==
       NULL) {
      fprintf(stderr, "%s\n", SDL_GetError());
      SDL_FreeSurface(tmp);
      return NULL;
   }

   SDL_FreeSurface(tmp);
   return ret;
}

image_t *
newImage(SDL_Surface * surface)
{
   image_t *new;

   assert(surface != NULL);

   new = malloc(sizeof(image_t));
   new->image = surface;
   new->w = surface->w;
   new->h = surface->h;

   return new;
}

static void
destroyImage(image_t * p)
{
   assert(p != NULL);
   SDL_FreeSurface((SDL_Surface *) p->image);
   free(p);
}

/*
 * Prida obrazok do globalneho zoznamu obrazkov
 * *file - nazov suboru v ktorom je obrazok ulozeny
 * *name - nazov obrazku ( pouzije sa ako vyhadavaci retazec v zazanem )
 * alpha - 0 - nema alpha kanal | 1 - ma alpha kanal
 */
image_t *
addImageData(char *file, int alpha, char *name, char *group)
{
   SDL_Surface *surface;
   image_t *new;

   assert(file != NULL);
   assert(name != NULL);
   assert(group != NULL);

   surface = loadImage(file, alpha);
   new = newImage(surface);

   addItemToStorage(listStorage, group, name, new);
#ifdef DEBUG
   printf(_("Loading image %s\n"), file);
#endif
   return new;
}

/*
 * Vrati odkaz na image_data v globalnom zozname obrazkov
 * a nazvom *s
 */
image_t *
getImage(char *group, char *name)
{
   assert(group != NULL);
   assert(name != NULL);

   return getItemFromStorage(listStorage, group, name);
}

void
delImage(char *group, char *name)
{
   assert(group != NULL);
   assert(name != NULL);

   delItemFromStorage(listStorage, group, name, destroyImage);
}

void
delAllImageInGroup(char *group)
{
   assert(group != NULL);

   delAllItemFromStorage(listStorage, group, destroyImage);
}

void
drawImage(image_t * p, int x, int y, int px, int py, int w, int h)
{
   static SDL_Surface *screen = NULL;
   SDL_Rect dst_rect, src_rect;

   if (screen == NULL) {
      screen = getSDL_Screen();
   }

   dst_rect.x = x;
   dst_rect.y = y;

   src_rect.x = px;
   src_rect.y = py;
   src_rect.w = w;
   src_rect.h = h;

   SDL_BlitSurface(p->image, &src_rect, screen, &dst_rect);
}

/*
 * Odstrani zoznam obrazkov z pamate
 */
void
quitImageData()
{
#ifdef DEBUG
   printf(_("Quitting image database\n"));
#endif
   destroyStorage(listStorage, destroyImage);
   isImageDataInit = FALSE;
}