![]() |
|
Blitting TGA with ALPHA channel (OGL) |
GullRaDriel
Member #3,861
September 2003
![]() |
I ) Introduction: I'm working on some functions to handle alpha channel and blit an animation of them. II ) What is my problem: I got a SPRITE structure where i keep information for my anims. Since i try to use AllegroGL instead of directX, i got the following problems: <b>III ) What i want to do :</b> 1: Make an initialize function to handle allegroGL for 2d iso using <b>IV ) so, WTF ?</b> I'm for sure don't understanding how do GL work. I'm ok to draw lines, point , shapes. But with alpha i got a real problem. Are function for direct use of BITMAP struct with allegroGL available ? If some can post code , i'll be very happy. <b>V ) Function & structs used</b> struct SPRITE: 1/*!
2 * type of a normal blitted sprite
3 */
4
5#define NORMAL_SPRITE 0
6
7
8
9/*!
10 * type of a masked sprite
11 */
12
13#define MASKED_SPRITE 1
14
15
16
17
18/*!
19 * type of a Transp sprite
20 */
21
22#define TRANS_SPRITE 2
23
24
25
26/*! Struct of a cell of an animation */
27typedef struct SPRITE {
28
29 /*! sprite */
30 BITMAP **spr;
31
32 int /*! w size */
33 w,
34 /*! h size */
35 h,
36 /*! x center of sprite */
37 x,
38 /*! y center of sprite */
39 y,
40 /*! id of the sprite -1 for unavailable */
41 id,
42 /* type of data ( with blending or just masking ) */
43 type,
44 /*! is animated ? true or false */
45 animated,
46 /*! delays between image -1 if no*/
47 delay,
48 /*! number of pictures in **spr */
49 current,
50 /*! max picture in **spr */
51 max,
52 /*! namelenght */
53 namelenght;
54
55 /*! handles to opengl textures if enable (not saved or loaded) */
56 GLuint *handles;
57
58
59 /*! name of sprite */
60 char *name;
61
62} SPRITE;
sprite saver: 1/*!\fn write_sprite_in_file( FILE *file , SPRITE *spr )
2 *
3 *\brief write a sprite in an opened file
4 *
5 *\param file an opening FILE *file to write in
6 *\param spr a SPRITE pointer to know the data
7 *
8 *\return FALSE if not valid file or writing error or memory error TRUE else
9 */
10
11int write_sprite_in_file( FILE *file , SPRITE *spr ) {
12
13 int it,it1,it2,
14 color;
15
16
17 /* error checking*/
18
19 if( !file ) return FALSE;
20
21 if( !spr ) return FALSE;
22
23 fwrite( &spr -> w , sizeof(int) , 1 , file );
24 fwrite( &spr -> h , sizeof(int) , 1 , file );
25 fwrite( &spr -> x , sizeof(int) , 1 , file );
26 fwrite( &spr -> y , sizeof(int) , 1 , file );
27 fwrite( &spr -> id , sizeof(int) , 1 , file );
28 fwrite( &spr -> type , sizeof(int) , 1 , file );
29 fwrite( &spr -> animated , sizeof(int) , 1 , file );
30 fwrite( &spr -> delay , sizeof(int) , 1 , file );
31 fwrite( &spr -> current , sizeof(int) , 1 , file );
32 fwrite( &spr -> max , sizeof(int) , 1 , file );
33 fwrite( &spr -> namelenght , sizeof(int) , 1 , file );
34
35
36 for( it = 0 ; it < spr -> namelenght ; it ++)
37 fwrite( &spr -> name[ it ] , sizeof(char) , 1 , file );
38
39
40 for( it = 0 ; it < spr -> current ; it ++ ) {
41
42 for( it1 = 0 ; it1 < spr -> w ; it1 ++ ) {
43
44 for( it2 = 0 ; it2 < spr -> h ; it2 ++ ) {
45
46 color = getpixel( spr -> spr[it] , it1 , it2 );
47
48 fwrite( &color , sizeof(int) , 1 , file );
49
50 }
51 }
52 }
53
54 return TRUE;
55
56
57}/* write_sprite_in_file( ... ) */
sprite loader: 1/*!\fn read_sprite_in_file( FILE *file , SPRITE **spr )
2 *
3 *\brief read a sprite in an opened file
4 *
5 *\param file an opening FILE *file to read in
6 *\param spr a SPRITE pointer to point the sprite in memory
7 *
8 *\return FALSE if not valid file or writing error or memory error TRUE else
9 */
10
11int read_sprite_in_file( FILE *file , SPRITE **spr ) {
12
13
14
15 int it,
16 it1,
17 it2,
18 color;
19
20
21
22 /* error checking*/
23
24 if( !file ) return FALSE;
25
26 init_sprite( spr , "name" ,-1,-1,-1,-1,-1,-1,-1,-1,-1);
27
28
29 if( !spr ) return FALSE;
30
31
32 fread( &(*spr) -> w , sizeof(int) , 1 , file );
33 fread( &(*spr) -> h , sizeof(int) , 1 , file );
34 fread( &(*spr) -> x , sizeof(int) , 1 , file );
35 fread( &(*spr) -> y , sizeof(int) , 1 , file );
36 fread( &(*spr) -> id , sizeof(int) , 1 , file );
37 fread( &(*spr) -> type , sizeof(int) , 1 , file );
38 fread( &(*spr) -> animated , sizeof(int) , 1 , file );
39 fread( &(*spr) -> delay , sizeof(int) , 1 , file );
40 fread( &(*spr) -> current , sizeof(int) , 1 , file );
41 fread( &(*spr) -> max , sizeof(int) , 1 , file );
42 fread( &(*spr) -> namelenght , sizeof(int) , 1 , file );
43
44 Malloc( (*spr) -> name , char , (*spr) -> namelenght + 1 );
45
46 fill_str( (*spr) -> name , 0 , (*spr) -> namelenght +1 );
47
48
49 for( it = 0 ; it < (*spr) -> namelenght ; it ++)
50 fread( &(*spr) -> name[ it ] , sizeof(char) , 1 , file );
51
52
53 Malloc( (*spr) -> spr , BITMAP * , (*spr) -> max );
54 Malloc( (*spr) -> handles , GLuint , (*spr) -> max );
55
56
57 for( it = 0 ; it < (*spr) -> current ; it ++ ) {
58
59 (*spr) -> spr[ it ] = create_bitmap( (*spr) -> w , (*spr) -> h );
60
61 clear_to_color( (*spr) -> spr[ it ] , makeacol( 0 , 0 , 0 , 0 ) );
62
63
64 for( it1 = 0 ; it1 < (*spr) -> w ; it1 ++ ) {
65
66
67 for( it2 = 0 ; it2 < (*spr) -> h ; it2 ++ ) {
68
69 fread( &color , sizeof(int) , 1 , file );
70
71 putpixel( (*spr) -> spr[ it ] , it1 , it2 , color );
72
73 }
74
75 }
76 (*spr) -> handles[ it ] = allegro_gl_make_texture( (*spr) -> spr[ it] );
77
78 }
79 return TRUE;
80
81
82 }/* read_sprite_in_file(...) */
sprite blitter: 1/*!\fn blit_sprite( SPRITE *sprite , int x , int y , int frame , BITMAP *bmp )
2 *
3 *\brief draw a sprite at the right position
4 *
5 *\param sprite a pointer where the data are
6 *\param x where to put sprite on BITMAP , X
7 *\param y where to put sprite on BITMAP , Y
8 *\param frame frame of sprite to blit
9 *\param bmp BITMAP object where blit sprite
10 *
11 *\return TRUE or FALSE (if not valid frame ie)
12 */
13
14int blit_sprite( SPRITE *sprite , int x , int y , int frame , BITMAP *bmp ) {
15
16
17 if( !sprite || frame < 0 || frame >= sprite -> current )return FALSE;
18
19 switch( sprite -> type ) {
20
21 /* normal blit */
22 case NORMAL_SPRITE :
23
24 if( bmp) stretch_blit( sprite -> spr[ frame ] , bmp ,
25 0 , 0 ,
26 sprite -> spr[ frame ] -> w ,
27 sprite -> spr[ frame ] -> h ,
28 x - sprite -> x ,
29 y - sprite -> y ,
30 sprite -> w,
31 sprite -> h
32 }
33 else {
34 /*
35 * TO DO: add Ogl blitter
36 */
37 }
38 );
39
40
41 return TRUE;
42
43 break;
44
45
46 /* masked blit */
47 case MASKED_SPRITE:
48 if( bmp ) masked_stretch_blit( sprite -> spr[ frame ] , bmp ,
49 0 , 0 ,
50 sprite -> spr[ frame ] -> w ,
51 sprite -> spr[ frame ] -> h ,
52 x - sprite -> x ,
53 y - sprite -> y ,
54 sprite -> w,
55 sprite -> h
56 );
57 else {
58 /*
59 * TO DO: add Ogl blitter
60 */
61 }
62
63 return TRUE;
64
65 break;
66
67
68 /* translucent blit */
69 case TRANS_SPRITE:
70
71 if( bmp ){
72 set_alpha_blender();
73 draw_trans_sprite( bmp , sprite -> spr[ frame ] , x - sprite -> x , y - sprite -> y );
74 }
75 else {
76 /*
77 * TO DO: add Ogl blitter
78 */
79 }
80
81
82 return TRUE;
83
84 break;
85
86
87 /* Sprite has no type ! */
88 default:
89
90 return FALSE;
91
92 break;
93
94 }/*switch(...)*/
95
96 return TRUE;
97
98 } /* blit_sprite(...) */
allegroGL initialiser: /* * TO DO : add initialize function */ allegroGL exit: /* * TO DO : add allegroGL exit function */ <b>Conclusion:</b> OpenGl make me crazy, i have not sleeped the last 3 night, i'm tired to search and obtain the same result . I need help. Thanks "Code is like shit - it only smells if it is not yours" |
Fladimir da Gorf
Member #1,565
October 2001
![]() |
Quote: OpenGl make me crazy, i have not sleeped the last 3 night, i'm tired to search and obtain the same result . I need help. Thanks What about OpenLayer? It's made for this kind of a thing - to render bitmaps with an alpha channel using OpenGL. OpenLayer has reached a random SVN version number ;) | Online manual | Installation video!| MSVC projects now possible with cmake | Now alvailable as a Dev-C++ Devpack! (Thanks to Kotori) |
GullRaDriel
Member #3,861
September 2003
![]() |
Perhaps because i wanna know how to do this using only allegro & allegrogl EDIT: But i'm also looking at openlayer. I'm not sure it has all the requirement i need , but i'll see. And if you know how to solve the topic with only allegro&allegrogl, don't wait and post your answer ! EDIT2: i'm looking at lots of code using allegroGL. The problem is that in fact , no one use the same organisation as the other. Some are calling allegro_gl_begin() ...end() where some others don't, to do the same thing. "Code is like shit - it only smells if it is not yours" |
Fladimir da Gorf
Member #1,565
October 2001
![]() |
Quote: Perhaps because i wanna know how to do this using only allegro & allegrogl Why? OpenLayer isn't a wrapper for AllegroGL. It maps the functions to direct OpenGL calls, not to AllegroGL code. Thus it enables a lot of effects and blending modes which aren't possible with AllegroGL's interface. Sorry, but I don't really know how to solve your problem. But in many cases if you wish to use pure OpenGL you could take a peek at OpenLayer's code. Also you wouldn't need any wrapper sprite class because OpenLayer automates all the code you posted. OpenLayer has reached a random SVN version number ;) | Online manual | Installation video!| MSVC projects now possible with cmake | Now alvailable as a Dev-C++ Devpack! (Thanks to Kotori) |
GullRaDriel
Member #3,861
September 2003
![]() |
is openlayer compatible with C code ? and ansi ? and finally does it run under linux ? "Code is like shit - it only smells if it is not yours" |
Fladimir da Gorf
Member #1,565
October 2001
![]() |
OpenLayer is in ansi C++ and platform independant. But in most cases you should be able to write C code but compile it in C++. OpenLayer has reached a random SVN version number ;) | Online manual | Installation video!| MSVC projects now possible with cmake | Now alvailable as a Dev-C++ Devpack! (Thanks to Kotori) |
Kitty Cat
Member #2,815
October 2002
![]() |
Quote: 1: never see the right color, always got various red colors. This leads me to believe you got software rendering. After you set the mode, try printing out these strings: printf("Graphics Driver: OpenGL %s (%s)\n" " %s detected\n" " Set %dx%d %dbpp %s mode\n", glGetString(GL_VERSION), glGetString(GL_VENDOR), glGetString(GL_RENDERER), SCREEN_W, SCREEN_H, bitmap_color_depth(screen), (gfx_driver->windowed ? "windowed" : "fullscreen")); If you are getting software rendering make sure you set and request GL_RENDERMETHOD 1. -- |
GullRaDriel
Member #3,861
September 2003
![]() |
Graphics Driver: OpenGL 2.0.0 (NVIDIA Corporation) GL_RENDERMETHOD is already requested. "Code is like shit - it only smells if it is not yours" |
Pavel Hilser
Member #5,788
April 2005
![]() |
About the wrong colors - i did the same thing, loading PCX, saving them back into my file format and then in my game - load my format back to bitmaps (and then converting into AllegoGL textures). BTW: how do you draw the textures ? I thing, if you do it the OpenGl way, you should have a lot of more FPS.. This is my basic drawing function (I'm really a beginner in openGl, but this will do all you need - rotation, alpha, rgb changes).
_____________________________________ |
GullRaDriel
Member #3,861
September 2003
![]() |
I test with your drawing function. What i do for saving and loading colors is:
WTF with this ? i can't see where it swap rgb to rbg or other. EDIT: i found this in the doc of allegroGL. Quote: Important note: on 32 bit bitmap in RGBA mode, the alpha channel created by Allegro is set to all 0 by default. This will cause the texture to not show up in 32bpp modes if alpha is set. You will need to fill in the alpha channel manually if you need an alpha channel. Help ! Ohé ! Bob Bombadil, Bob Bombadillon ! "Code is like shit - it only smells if it is not yours" |
Pavel Hilser
Member #5,788
April 2005
![]() |
what bpp you're using when saving ? and when using OGL ? is it the same ? _____________________________________ |
GullRaDriel
Member #3,861
September 2003
![]() |
bpp is the same at loading & saving. it's 32 bpp. If someone have time to help, i have attached some tga that i can be able to quickly blit using allegro&agl. "Code is like shit - it only smells if it is not yours" |
Bob
Free Market Evangelist
September 2000
![]() |
Would be nice if you posted some code, on occasion. -- |
GullRaDriel
Member #3,861
September 2003
![]() |
Here i made an example, who works very slow. you can download it the source code is posted here too agl.c:
It's working, good colors, but it is sloooow and the drivers is good. EDIT: i'm allowed myself to cut&paste your 5 lines of code, kittycat, and the return is always : EDIT2: to test it, launch a command, and go to the atb directory, launch atb 35 (there are 35 tga in /DATA) EDIT3: can't be able to speed this up... can it be a size issue? lemme check. EDIT4: I test all examples in AllegroGL examples directory. Results are: exmasked is as slow as the pasted code. WTF !!²²!! EDIT5: What can be the raison for exmasked to be so slow ? it is right for colors, but it is already so slow... !!! IT WAS A DRIVER ISSUE !!! Nvidia's Update for GeforceFX Go 5700 are bad ... everything is better now Sorry to have make you loose times ... can someone post for me to give cookies ? "Code is like shit - it only smells if it is not yours" |
BAF
Member #2,981
December 2002
![]() |
Bump |
GullRaDriel
Member #3,861
September 2003
![]() |
And no thanks to all for helping out (BAF is coockied because i bore him a lot on msn all along the day "Code is like shit - it only smells if it is not yours" |
|