-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathResNDraw
More file actions
182 lines (167 loc) · 5.69 KB
/
Copy pathResNDraw
File metadata and controls
182 lines (167 loc) · 5.69 KB
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
#ifndef RESOURCEMANAGER_C
#define RESOURCEMANAGER_C
#include "Library.h"
#include "Artist.c"
#include <stdio.h>
static const char* HOME = "C:\\!ARBO\\";
typedef unsigned short WORD;
#pragma pack(push, 2)
typedef struct _BMPheader
{
WORD bfType; /* Signature: 0x4d42 | 0x4349 | 0x5450*/
int bfSize; /* File size*/
int bfReserved; /* Always 0*/
int bfOffBits; /* Offset to the data*/
/* Usually 54 = 16 + biSize*/
int biSize; /* Structure size:*/
/* 40(BITMAPINFOHEADER) or 108(BITMAPV4HEADER)*/
/* or 124(BITMAPV5HEADER)*/
int biWidth; /* Width*/
int biHeight; /* Height*/
WORD biPlanes; /* Always 1*/
WORD biBitCount; /* 0 | 1 | 4 | 8 | 16 | 24 | 32*/
int biCompression; /* BI_RGB | BI_RLE8 | BI_RLE4 |*/
/* BI_BITFIELDS | BI_JPEG | BI_PNG*/
/* The most common is BI_RGB*/
int biSizeImage; /* Image data byte count*/
/* Usually 0*/
int biXPelsPerMeter;/* horizontal resolution*/
int biYPelsPerMeter;/* vertical resolution*/
int biClrUsed; /* Amount of colors used*/
/* (if color table used)*/
int biClrImportant; /* Amount of important colors.*/
/* Usually 0*/
}BMPheader;
#pragma pack(pop)
char *loadBMP(struct _AUX_RGBImageRec* img, const char* path)
{
int row_leng, my;
FILE *f = fopen(path, "rb");
if (!f) return NULL;
BMPheader bh;
fread(&bh, sizeof(bh), 1, f);
/* проверяем сигнатуру*/
if (bh.bfType != 0x4d42 && bh.bfType != 0x4349 && bh.bfType != 0x5450) { fclose(f); return NULL; }
/* восстановим указатель в файле:*/
fseek(f, sizeof(BMPheader), SEEK_SET);
/* проверим условия*/
if (bh.bfReserved != 0 ||
bh.biPlanes != 1 ||
(bh.biSize != 40 && bh.biSize != 108 && bh.biSize != 124) ||
bh.bfOffBits != 14 + bh.biSize ||
bh.biWidth < 1 || bh.biWidth >10000 ||
bh.biHeight < 1 || bh.biHeight>10000 ||
bh.biBitCount != 24 ||
bh.biCompression != 0
)
{
fclose(f);
return NULL;
}
row_leng = bh.biWidth *3;
my = bh.biHeight;
img->sizeX = bh.biWidth;
img->sizeY = my;
unsigned char *tmp_buf = malloc(sizeof(unsigned char)*bh.biHeight*row_leng);
int y = 0;
int x = 0;
int oldX = 0;
int skip = bh.biWidth % 4;
for (y = 0; y < my; y++)
{
for (x = oldX; x < oldX + row_leng; x += 3)
{
fread(tmp_buf + x + 2, sizeof(unsigned char), 1, f);
fread(tmp_buf + x + 1, sizeof(unsigned char), 1, f);
fread(tmp_buf + x, sizeof(unsigned char), 1, f);
}
oldX = x;
fseek(f, sizeof(char) * skip, SEEK_CUR);
}
fclose(f);
return tmp_buf;
}
const char* buildString(char* name)
{
char* path = malloc(sizeof(char)*(strlen(HOME) + strlen(name)) + 4);
strcpy(path, HOME);
strcat(path, name);
strcat(path, ".bmp");
return path;
}
void loadNewImage(char* name)
{
main_array.length++;
mainArray = mainArray == NULL ? (struct _AUX_RGBImageRec*)realloc(NULL, sizeof(struct _AUX_RGBImageRec)) : (struct _AUX_RGBImageRec*)realloc(mainArray, sizeof(struct _AUX_RGBImageRec)*(main_array.length + 1));
(mainArray + main_array.length)->data = loadBMP((mainArray + main_array.length), buildString(name));
}
void saveData()
{
int i = 0;
char len;
FILE* datafile = fopen("C:\\!ARBO\\data.bin", "wb");
fwrite(&gmlen, sizeof(int), 1, datafile);
for (i = 0; StringPool[i] != "/*" && i < 99; i++)
{
len = (char)strlen(StringPool[i]);
fwrite(&len, sizeof(char), 1, datafile);
fputs(StringPool[i], datafile);
/*fputc('\0', datafile);*/
}
fputc('\0', datafile);
for (i = 0; i < gmlen; i++)
{
fwrite(&GameObjects[i]->busy, sizeof(unsigned char), 3, datafile);
/*for (g = 0; g < GameObjects[i]->animations; g++)*/
fwrite(GameObjects[i]->animation, sizeof(struct model), GameObjects[i]->animations, datafile);
fwrite(&GameObjects[i]->x, sizeof(int), 5, datafile);
fwrite(&GameObjects[i]->vector, sizeof(unsigned char), 3, datafile);
}
fclose(datafile);
}
void readData()
{
int i = 0 , g=0;
char len;
FILE* datafile = fopen("C:\\!ARBO\\data.bin", "rb");
struct _GameObject* test;
struct model* animations;
fread(&gmlen, sizeof(int), 1, datafile);
GameObjects = malloc(sizeof(int) * gmlen);
test = malloc(sizeof(struct _GameObject) * gmlen);
animations = malloc(sizeof(struct model)* gmlen * 3);
StringPool = malloc(sizeof(char*) * 100);
main_array.length = -1;
for (i = 0; i < 99; i++)
{
len = fgetc(datafile);
StringPool[i] = malloc(sizeof(char)*len);
fgets(StringPool[i], len+1, datafile);
printf("%s\n", StringPool[i]);
if (StringPool[i][0] == '\0') break;
loadNewImage(StringPool[i]);
}
for (i = 0; i < gmlen; i++)
{
fread(&test[i].busy, sizeof(unsigned char), 3, datafile);
test[i].animation = malloc(sizeof(struct model)*test[i].animations);
fread(test[i].animation, sizeof(struct model), test[i].animations, datafile);
fread(&test[i].x, sizeof(int), 5, datafile);
fread(&test[i].vector, sizeof(unsigned char), 3, datafile);
g += test[i].animations;
GameObjects[i] = &test[i];
for (g = 0; g < test[i].animations; g++)
test[i].animation[g].frame = mainArray[test[i].animation[g].id].data;
}
for (i = 0; i < gmlen; i++)
{
GameObjects[i] = &test[i];
printf("Object #%d\nbusy: %d\ncurrent_animation: %d\nanimations %d\nx %d\ny: %d\n", (i + 1), test[i].busy, test[i].current_animation, test[i].animations, test[i].x, test[i].y);
for (g = 0; g < test[i].animations; g++)
printf("\tHis animation:\n\t\tid: %d\n\t\tmsecs: %d\n\t\tframes: %d\n\t\twidth: %d\n\n", test[i].animation[g].id, test[i].animation[g].msecs, test[i].animation[g].frames, test[i].animation[g].width);
}
for (i = 0; i <= main_array.length; i++)
printf("Width: %d; Height: %d\n", mainArray[i].sizeX, mainArray[i].sizeY);
fclose(datafile);
}
#endif /* !RESOURCEMANAGER_C*/