SDL Texture Relations

Started by
1 comment, last by Jay Pascua 9 years, 1 month ago

Good Afternoon guys, just had a quick question regarding SDL and Textures.

I've used SDL_Textures in Pong, Breakout, and now I'm using them in Tetris and it's not working the way that I expected at all. I just wanted to verify what I atleast think I know, I did try to verify on lazyfoo, as well as the wiki to no avail.

Also asides from the true or false, I would greatly appreciate a short explanation if possible, even just a sentence, hell doesn't even have to be a complete sentence as long as the point gets across. This is pretty much the way I've been using them and it's inconsistency in results obviously leads me to believe that I'm using them wrong. Thank you again in advance.

1.) SDL_Textures work pretty much like surfaces except that it's stored on the video card.

2.) SDL_Textures are pretty much like a layer equivalent in Photoshop? I've used the SDL_SetRenderTarget to draw all the walls of my Breakout game and rendered the texture and all 3 walls showed up.

Okay those were the ones I'm pretty positive, but could be wrong, here's the ones that I've really been running on assumptions on:

3.) I've been:


tBlockTexture = SDL_CreateTexture(_renderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, Tetromino::_blockWidth * blockSide, Tetromino::_blockHeight * blockSide);
SDL_SetTextureBlendMode(tBlockTexture, SDL_BLENDMODE_BLEND);

Setting the TextureBlendMode once, just right after creation, and I'm assuming that it persists until told otherwise?

4.)


	for (int i = 0; i < 3; i++) {
		for (int j = 0; j < 3; j++) {
			if (_tBlock[i][j] == 1) {
				SDL_Rect tBlock = { _xPos + (i*Tetromino::_blockWidth), _yPos + (j*Tetromino::_blockHeight), Tetromino::_blockWidth, Tetromino::_blockHeight };
				SDL_SetRenderDrawColor(_renderer, 255, 0, 0, 255);
				SDL_RenderFillRect(_renderer, &tBlock);
				SDL_SetRenderTarget(_renderer, tBlockTexture);
			}
		}
	}

In the other games I only had to deal with 1 rect per object, in pong the paddle, and the ball. So I could save a pointer to the object and manipulate it that way, but in this case since each Tetronimo is made up of numerous rects, I'm sorta lost. The order of interaction throws me off here completely. Originally I had the SetRenderTarget above SetRenderDrawColor and RenderFillRect, the way I pictured it in my head:

Quote

Me to SDL: I want you to create a Rect.
Me to SDL: Here's the paper where you're going to draw it on (SetRenderTarget)

Me to SDL: Here's the crayon (SetRenderDrawColor)

Me to SDL: Okay go ahead and draw it and draw it and fill it in (RenderFillRect)

So he draws that square on that paper, then does the loop again, I assumed that I don't need to save each rect, since it's being drawn to the texture and the whole texture is drawn to the screen later with the SDL_RenderPresent call. That unfortunately did not draw the square to the screen. ( Regardless only one square was drawn even though the array was initialized with:

_tBlock[0][1] = 1;

_tBlock[1][1] = 1;

_tBlock[1][2] = 1;

_tBlock[2][1] = 1;

I did originally try setting the array in the constructor using T_Block() : _tBlock{{0,1,0},{0,1,1},{0,1,0}}. Unfortuately from what I read on StackOverflow, that's a no go.

Well regardless of the 1 square thing, I don't want to bother you with that since I didn't really spend too much time looking into that, just wanted to make sure my understanding of how rendering/textures worked was correct. Thank you in advance.

Advertisement
1) Yes
2) Not sure if you could compare those like that, because textures don't have position or anything, they are just "images". Maybe you could think them as a brushes?
3) Seems right
4) So, you could generate the images for all object types and then create textures(or maybe one bigger texture) from those. But I would just create a single texture for one small tile and render that with SDL_RenderCopy multiple times to correct places on the screen.

Derp

1) Yes
2) Not sure if you could compare those like that, because textures don't have position or anything, they are just "images". Maybe you could think them as a brushes?
3) Seems right
4) So, you could generate the images for all object types and then create textures(or maybe one bigger texture) from those. But I would just create a single texture for one small tile and render that with SDL_RenderCopy multiple times to correct places on the screen.


Thank you

This topic is closed to new replies.

Advertisement