/[cvs]/wurmi/SDLutils.c
ViewVC logotype

Contents of /wurmi/SDLutils.c

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.2 - (show annotations)
Thu Sep 6 11:29:57 2007 UTC (11 years, 1 month ago) by kralovic
Branch: MAIN
CVS Tags: HEAD
Changes since 1.1: +1 -3 lines
File MIME type: text/plain
Dalsie zmeny

1 /*
2 * *******************************************************
3 * helper functions for accessing SDL
4 * *******************************************************
5 */
6
7 #include "SDLutils.h"
8
9 extern int verbose;
10
11 /*
12 * generic error message
13 */
14 void ERROR(const char*fmt,...) {
15 va_list ap;
16 va_start(ap, fmt);
17 fprintf(stderr,"Fatal error: ");fflush(stderr);
18 vfprintf(stderr,fmt,ap);
19 va_end(ap);
20 exit(1);
21 }
22
23 /*
24 * init_video
25 */
26 SDL_Surface * init_video(SDLOPT *opt) {
27 SDL_Surface * screen;
28
29 if (verbose) printf("Initializing SDL...");
30 if((SDL_Init(SDL_INIT_VIDEO|SDL_INIT_AUDIO|SDL_INIT_TIMER)==-1)) {
31 ERROR("could not initialize SDL: %s.\n", SDL_GetError());
32 exit(-1);
33 }
34 if (verbose) printf("done\n");
35
36 if (verbose) printf("Initializing video mode %dx%dx%d ...",opt->video_width,opt->video_height, opt->video_bpp);
37 screen = SDL_SetVideoMode(opt->video_width,opt->video_height, opt->video_bpp, opt->video_options);
38 if (screen==NULL) {
39 if (verbose)
40 printf("video mode %dx%dx%d failed [%s] - trying defaults...",opt->video_width,opt->video_height, opt->video_bpp,SDL_GetError());
41 screen = SDL_SetVideoMode(640, 480, 8, SDL_SWSURFACE);
42 if ( screen == NULL ) {
43 ERROR("video mode 640x480x8 failed [%s]\n", SDL_GetError());
44 exit(1);
45 }
46 }
47 if (verbose) printf("done\n");
48
49 if (verbose) printf("Initializing font system...");
50 if (TTF_Init()==-1) {
51 ERROR("failed to initialize font support (SDL_TTF).\n");
52 exit(-1);
53 }
54 if (verbose) printf("done\n");
55 return screen;
56 }
57
58 /*
59 * Set the pixel at (x, y) to the given value
60 * NOTE: The surface must be locked before calling this!
61 */
62 void putpixel(SDL_Surface *surface, int x, int y, Uint32 value) {
63 Uint32 pixel;
64 int bpp = surface->format->BytesPerPixel;
65 /* Here p is the address to the pixel we want to set */
66 Uint8 *p = (Uint8 *)surface->pixels + y * surface->pitch + x * bpp;
67
68 pixel=SDL_MapRGB(surface->format, (value&0xff0000)>>16, (value&0xff00)>>8, value&0xff);
69 switch(bpp) {
70 case 1: *p = pixel; break;
71 case 2: *(Uint16 *)p = pixel; break;
72 case 3:
73 if(SDL_BYTEORDER == SDL_BIG_ENDIAN) {
74 p[0] = (pixel >> 16) & 0xff;
75 p[1] = (pixel >> 8) & 0xff;
76 p[2] = pixel & 0xff;
77 } else {
78 p[0] = pixel & 0xff;
79 p[1] = (pixel >> 8) & 0xff;
80 p[2] = (pixel >> 16) & 0xff;
81 }
82 break;
83 case 4: *(Uint32 *)p = pixel; break;}
84 }
85
86
87 /*
88 * load_jpeg
89 */
90 SDL_Surface *load_jpeg(char *filename) {
91 struct jpeg_decompress_struct cinfo;
92 struct jpeg_error_mgr jerr;
93 FILE * infile;
94 /*JSAMPARRAY buffer;*/
95 JSAMPROW row;
96 int row_stride,i,j;
97 unsigned char *image,*iptr;
98
99
100 cinfo.err = jpeg_std_error(&jerr);
101 jpeg_create_decompress(&cinfo);
102 infile=fopen(filename,"rb");
103 jpeg_stdio_src(&cinfo, infile);
104 jpeg_read_header(&cinfo, TRUE);
105
106 cinfo.out_color_space = JCS_RGB;
107
108 jpeg_start_decompress(&cinfo);
109
110 image=(unsigned char*)malloc(cinfo.output_components*cinfo.output_width*cinfo.output_height);
111
112 row_stride = cinfo.output_width * cinfo.output_components;
113 /* buffer = (*cinfo.mem->alloc_sarray)
114 ((j_common_ptr) &cinfo, JPOOL_IMAGE, row_stride, 1);*/
115
116 for (iptr=image;cinfo.output_scanline < cinfo.output_height;) {
117 /* jpeg_read_scanlines(&cinfo,buffer,1);
118 for(i=0;i<row_stride;i++)
119 *(iptr++)=buffer[0][i];*/
120 row=image+(cinfo.output_scanline*row_stride);
121 jpeg_read_scanlines(&cinfo,&row,1);
122 }
123
124 jpeg_finish_decompress(&cinfo);
125 jpeg_destroy_decompress(&cinfo);
126 fclose(infile);
127
128 if(verbose)
129 printf("Loaded image \"%s\" (%dx%d)\n",filename,cinfo.output_width,cinfo.output_height);
130
131 return SDL_CreateRGBSurfaceFrom((void *)image, cinfo.output_width,cinfo.output_height, 24,
132 row_stride, 0xff0000,0x00ff00,0x0000ff,0);
133 }
134
135 /*
136 * load_png
137 */
138 SDL_Surface *load_png(char *filename) {
139
140 png_structp png_ptr;
141 png_infop info_ptr;
142 unsigned int sig_read = 0;
143 png_uint_32 width, height;
144 int bit_depth, color_type, interlace_type;
145 FILE *fp;
146 unsigned char *image;
147 png_bytep *row_pointers;
148 int i,w,h,rb;
149 SDL_Surface *result;
150
151 fp = fopen(filename, "rb");
152 png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING,0,0,0);
153 info_ptr = png_create_info_struct(png_ptr);
154 setjmp(png_jmpbuf(png_ptr));
155 png_init_io(png_ptr, fp);
156 png_read_info(png_ptr, info_ptr);
157 w=info_ptr->width;
158 h=info_ptr->height;
159 rb=info_ptr->rowbytes;
160 image=(unsigned char*)malloc(h*rb);
161
162 row_pointers=(png_bytep *)malloc(h*sizeof(png_bytep));
163 for (i=0;i<h;i++)
164 row_pointers[i]=image+i*rb;
165 png_set_rows(png_ptr, info_ptr, &row_pointers);
166
167 png_read_image(png_ptr,row_pointers);
168
169
170 result= SDL_CreateRGBSurfaceFrom((void *)image, info_ptr->width,info_ptr->height, info_ptr->pixel_depth,
171 info_ptr->rowbytes,
172 ( (1<<(info_ptr->bit_depth)) -1) * (info_ptr->channels-3),
173 ( (1<<(info_ptr->bit_depth)) -1) << ((info_ptr->channels-3)*(info_ptr->bit_depth)),
174 ( (1<<(info_ptr->bit_depth)) -1) << ((info_ptr->channels-2)*(info_ptr->bit_depth)),
175 ( (1<<(info_ptr->bit_depth)) -1) << ((info_ptr->channels-1)*(info_ptr->bit_depth))
176 );
177
178 png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp)NULL);
179 free(row_pointers);
180 fclose(fp);
181 return result;
182
183 }
184
185
186 /*
187 * parameters: x,y, source, destination
188 * blits the source to the destination with offset x,y
189 */
190
191 void apply_surface(SDL_Surface* source, SDL_Rect *clip, SDL_Surface* destination, int x,int y) {
192 //Holds offsets
193 SDL_Rect offset;
194
195 //Get offsets
196 offset.x = x;
197 offset.y = y;
198
199 //Blit
200 SDL_BlitSurface( source, clip, destination, &offset );
201 }
202
203

CVS Admin">CVS Admin
ViewVC Help
Powered by ViewVC 1.1.26