TheExDeus
|
|
Posted on: September 10, 2010, 05:59:44 am |
|
|
Joined: Apr 2008
Posts: 1860
|
Notice: These functions work only with power of two textures (sprites), so this implementation is kind of limited. Also, it will only draw a tiled region with width/height the length of min(room_width,room_height). This is OpenGL limitation. Still, this could be used for something, and I think tiling power of two images this way is much faster than the other way ( http://enigma-dev.org/forums/index.php?topic=641.0), but that way doesn't have any limitations. Function: draw_sprite_tiled( int spr, int subimg, double x, double y); GSsprite.h: int draw_sprite_tiled(int spr,int subimg,double x,double y); GSsprite.cpp: int draw_sprite_tiled(int spr,int subimg,double x,double y) { enigma::sprite *spr2d = enigma::spritestructarray[spr]; if (!spr2d) return -1;
if (enigma::cur_bou_tha_noo_sho_eve_cha_eve != spr2d->texturearray[subimg % spr2d->subcount]) { glBindTexture(GL_TEXTURE_2D,spr2d->texturearray[subimg % spr2d->subcount]); enigma::cur_bou_tha_noo_sho_eve_cha_eve = spr2d->texturearray[subimg % spr2d->subcount]; }
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_REPEAT); glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_REPEAT);
glPushAttrib(GL_CURRENT_BIT); glColor4f(1,1,1,1);
const float tbx=spr2d->texbordx,tby=spr2d->texbordy, vertil=room_width/spr2d->width, hortil=room_height/spr2d->height, xoff=spr2d->xoffset/spr2d->width+x/spr2d->width, yoff=spr2d->yoffset/spr2d->height+y/spr2d->height; glBegin(GL_QUADS); glTexCoord2f(xoff,yoff); glVertex2f(0,0); glTexCoord2f(hortil*tbx+xoff,yoff); glVertex2f(spr2d->width*hortil,0); glTexCoord2f(hortil*tbx+xoff,vertil*tby+yoff); glVertex2f(spr2d->width*hortil,spr2d->height*vertil); glTexCoord2f(xoff,vertil*tby+yoff); glVertex2f(0,spr2d->height*vertil); glEnd();
glPopAttrib(); return 0; } Function:draw_sprite_tiled_ext( int spr, int subimg, double x, double y, double xscale, double yscale, int color, double alpha); GSsprite.h: int draw_sprite_tiled_ext(int spr,int subimg,double x,double y,double xscale,double yscale,int color,double alpha); GSsprite.cpp: int draw_sprite_tiled_ext(int spr,int subimg,double x,double y,double xscale,double yscale,int blend,double alpha) { enigma::sprite *spr2d = enigma::spritestructarray[spr]; if (!spr2d) return -1;
if (enigma::cur_bou_tha_noo_sho_eve_cha_eve != spr2d->texturearray[subimg % spr2d->subcount]) { glBindTexture(GL_TEXTURE_2D,spr2d->texturearray[subimg % spr2d->subcount]); enigma::cur_bou_tha_noo_sho_eve_cha_eve = spr2d->texturearray[subimg % spr2d->subcount]; }
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_REPEAT); glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_REPEAT);
glPushAttrib(GL_CURRENT_BIT); //glColor4f(1,1,1,1); glColor4ub(__GETR(blend),__GETG(blend),__GETB(blend),char(alpha*255)); const float tbx=spr2d->texbordx,tby=spr2d->texbordy, vertil=room_width/(spr2d->width*xscale), hortil=room_height/(spr2d->height*yscale), xoff=spr2d->xoffset/(spr2d->width*xscale)+x/(spr2d->width*xscale), yoff=spr2d->yoffset/(spr2d->height*yscale)+y/(spr2d->height*yscale); glBegin(GL_QUADS); glTexCoord2f(xoff,yoff); glVertex2f(0,0); glTexCoord2f(hortil*tbx+xoff,yoff); glVertex2f(spr2d->width*xscale*hortil,0); glTexCoord2f(hortil*tbx+xoff,vertil*tby+yoff); glVertex2f(spr2d->width*xscale*hortil,spr2d->height*yscale*vertil); glTexCoord2f(xoff,vertil*tby+yoff); glVertex2f(0,spr2d->height*yscale*vertil); glEnd();
glPopAttrib(); return 0; } TO-DO: 1) Make both functions work in views (now they use room size) 2) Tile the tiled images. Now it draws as a square which has side as min(room_width, room_height). Maybe someone more profound in C++ could help with this.
|
|
« Last Edit: September 25, 2010, 04:53:23 pm by HaRRiKiRi »
|
Logged
|
|
|
|
Josh @ Dreamland
|
|
Reply #1 Posted on: September 10, 2010, 12:10:32 pm |
|
|
Prince of all Goldfish
Location: Pittsburgh, PA, USA Joined: Feb 2008
Posts: 2950
|
'fraid I have to deny those. They're unimplemented for a reason. Create a 48*48 sprite and try your functions; you won't like the results. But for power-of-two textures, that will work fine. See the issue?
|
|
|
Logged
|
"That is the single most cryptic piece of code I have ever seen." -Master PobbleWobble "I disapprove of what you say, but I will defend to the death your right to say it." -Evelyn Beatrice Hall, Friends of Voltaire
|
|
|
|
Josh @ Dreamland
|
|
Reply #3 Posted on: September 10, 2010, 02:45:18 pm |
|
|
Prince of all Goldfish
Location: Pittsburgh, PA, USA Joined: Feb 2008
Posts: 2950
|
Works for me. I figure at some point I'll have to break down and add an if() for it, anyway.
|
|
|
Logged
|
"That is the single most cryptic piece of code I have ever seen." -Master PobbleWobble "I disapprove of what you say, but I will defend to the death your right to say it." -Evelyn Beatrice Hall, Friends of Voltaire
|
|
|
|
|
|
|
|
Josh @ Dreamland
|
|
Reply #9 Posted on: January 11, 2011, 12:58:24 pm |
|
|
Prince of all Goldfish
Location: Pittsburgh, PA, USA Joined: Feb 2008
Posts: 2950
|
I don't think two branches are unreasonable.
HaRRi, note two things. First, tbx and tby are both 1 for Po2 textures. Second, I believe the most efficient way to code this function is by giving it this structure:
if (tbx == 1) if (tby == 1) //YOUR CURRENT METHOD else //DRAW LARGE TILED HORIZONTAL STRIPS OF SIZE REQUESTED_WIDTH * SPRITE_HEIGHT else if (tby == 1) //DRAW LARGE TILED VERTICAL STRIPS OF SIZE SPRITE_WIDTH * REQUESTED_HEIGHT else //THE INEFFICIENT METHOD
|
|
|
Logged
|
"That is the single most cryptic piece of code I have ever seen." -Master PobbleWobble "I disapprove of what you say, but I will defend to the death your right to say it." -Evelyn Beatrice Hall, Friends of Voltaire
|
|
|
TheExDeus
|
|
Reply #10 Posted on: January 11, 2011, 03:19:14 pm |
|
|
Joined: Apr 2008
Posts: 1860
|
First, tbx and tby are both 1 for Po2 textures. I am aware. But as I stated. (GL_TEXTURE_WRAP_S,GL_REPEAT) draws only the min(window_width,window_height). Maybe there is another parameter that needs to be changed, but for now I can't see any way to fix this. For example, I added a picture on how draw_sprite_tiled_n2 draws. It should tile the whole room, but it tiles a square of 480x480. Second, I believe the most efficient way to code this function is by giving it this structure: Well keeping the second thing in mind it should be: max_size = min(window_width,window_height) if (tbx == 1 && region_width < max_size) if (tby == 1 && region_height < max_size) //YOUR CURRENT METHOD else if (region_width < max_size) //DRAW LARGE TILED HORIZONTAL STRIPS OF SIZE REQUESTED_WIDTH * SPRITE_HEIGHT else if (tby == 1 && region_height < max_size) //DRAW LARGE TILED VERTICAL STRIPS OF SIZE SPRITE_WIDTH * REQUESTED_HEIGHT else //THE INEFFICIENT METHOD
|
|
|
Logged
|
|
|
|
Josh @ Dreamland
|
|
Reply #11 Posted on: January 11, 2011, 03:25:40 pm |
|
|
Prince of all Goldfish
Location: Pittsburgh, PA, USA Joined: Feb 2008
Posts: 2950
|
I don't know why it's doing that if you're specifying the whole screen as the rectangle.
|
|
|
Logged
|
"That is the single most cryptic piece of code I have ever seen." -Master PobbleWobble "I disapprove of what you say, but I will defend to the death your right to say it." -Evelyn Beatrice Hall, Friends of Voltaire
|
|
|
|
Josh @ Dreamland
|
|
Reply #13 Posted on: January 12, 2011, 10:39:30 am |
|
|
Prince of all Goldfish
Location: Pittsburgh, PA, USA Joined: Feb 2008
Posts: 2950
|
I have yet to implement switch(), but there are multiple problems with that code in any case, namely, 1) The point of a switch statement is to allow jumping to a piece of code marked by the correct value. So, "case varname==0" is pointless. Just "case 0" will suffice 2) The point of a switch statement is to quickly decode one value as a set of multiple values. So, "switch(boolean)" is, before optimization, less efficient than if (boolean) {} else {}.
|
|
|
Logged
|
"That is the single most cryptic piece of code I have ever seen." -Master PobbleWobble "I disapprove of what you say, but I will defend to the death your right to say it." -Evelyn Beatrice Hall, Friends of Voltaire
|
|
|
MrGriggs
|
|
Reply #14 Posted on: January 12, 2011, 10:53:18 am |
|
|
Joined: Dec 2010
Posts: 128
|
I have yet to implement switch(), but there are multiple problems with that code in any case, namely, 1) The point of a switch statement is to allow jumping to a piece of code marked by the correct value. So, "case varname==0" is pointless. Just "case 0" will suffice 2) The point of a switch statement is to quickly decode one value as a set of multiple values. So, "switch(boolean)" is, before optimization, less efficient than if (boolean) {} else {}.
I know already what you're saying, sorry, I put the switch in there for another reason I've yet to utilize... sorry for confusion
|
|
|
Logged
|
|
|
|
|