File: Utils.cpp

package info (click to toggle)
maelstrom 1.4.3-L2.0.6-13
  • links: PTS
  • area: non-free
  • in suites: potato
  • size: 4,352 kB
  • ctags: 2,231
  • sloc: cpp: 22,044; ansic: 1,853; sh: 242; makefile: 223
file content (121 lines) | stat: -rw-r--r-- 2,860 bytes parent folder | download | duplicates (3)
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


#include <stdlib.h>
#include <stdio.h>
#include "bitesex.h"
#include "framebuf.h"

extern FrameBuf *win;				/* From init.cpp */
extern char *file2libpath(char *file);		/* From shared.cpp */


int Load_Title(struct Title *title, int title_id)
{
	FILE *title_fp;
	unsigned char *pixels;
	char filename[256];
	
	/* Open the title file.. */
	sprintf(filename, "Images/Maelstrom_Titles#%d.icon", 
							title_id);
	if ( (title_fp=fopen(file2libpath(filename), "r")) == NULL )
		return(-1);
	fread(&title->width, 2, 1, title_fp);
	bytesexs(title->width);
	fread(&title->height, 2, 1, title_fp);
	bytesexs(title->height);
	pixels = new unsigned char[title->width*title->height];
	if ( ! fread(pixels, title->width*title->height, 1, title_fp) )
		return(-1);
	(void) fclose(title_fp);
	win->ReColor(pixels, &title->data, title->width*title->height);
	delete[] pixels;
	return(0);
}
void Free_Title(struct Title *title)
{
	win->FreeArt(title->data);
}

CIcon *GetCIcon(short cicn_id)
{
	CIcon         *cicn;
	FILE          *cicn_fp;
	unsigned char *pixels;
	char           filename[256];
	
	/* Open the cicn sprite file.. */
	sprintf(filename, "Images/Maelstrom_Icon#%hd.cicn", 
							cicn_id);
	if ( (cicn_fp=fopen(file2libpath(filename), "r")) == NULL ) {
		error("GetCIcon(%hd): Can't open CICN %s: ",
					cicn_id, file2libpath(filename));
		perror("");
		return(NULL);
	}

	cicn = new CIcon;
        fread(&cicn->width, sizeof(cicn->width), 1, cicn_fp);
	bytesexs(cicn->width);
        fread(&cicn->height, sizeof(cicn->width), 1, cicn_fp);
	bytesexs(cicn->height);
        pixels = new unsigned char[cicn->width*cicn->height];
        if ( fread(pixels, 1, cicn->width*cicn->height, cicn_fp) != 
						cicn->width*cicn->height) {
		error("GetCIcon(%hd): Corrupt CICN!\n", cicn_id);
		delete[] pixels;
		delete   cicn;
		return(NULL);
	}
        cicn->mask=new unsigned char[(cicn->width/8)*cicn->height];
        if ( fread(cicn->mask, 1, (cicn->width/8)*cicn->height, cicn_fp) != 
					((cicn->width/8)*cicn->height) ) {
		error("GetCIcon(%hd): Corrupt CICN!\n", cicn_id);
		delete[] pixels;
		delete[] cicn->mask;
		delete   cicn;
		return(NULL);
	}
	(void) fclose(cicn_fp);

	win->ReColor(pixels, &cicn->pixels, cicn->width*cicn->height);
	delete[] pixels;
	return(cicn);
}

void BlitCIcon(int x, int y, CIcon *cicn)
{
	win->Blit_Icon(x, y, cicn->width, cicn->height, 
						cicn->pixels, cicn->mask);
}

void FreeCIcon(CIcon *cicn)
{
	win->FreeArt(cicn->pixels);
	delete[] cicn->mask;
	delete   cicn;
}

void SetRect(Rect *R, int left, int top, int right, int bottom)
{
	R->left = left;
	R->top = top;
	R->right = right;
	R->bottom = bottom;
}

void OffsetRect(Rect *R, int x, int y)
{
	R->left += x;
	R->top += y;
	R->right += x;
	R->bottom += y;
}

void InsetRect(Rect *R, int x, int y)
{
	R->left += x;
	R->top += y;
	R->right -= x;
	R->bottom -= y;
}