Drawing a chess board with SDL2's SDL_RenderDrawRect - c++

I am fairly new to SDL2 and as my first project I wanted to just create a chess board. This has proven to be harder than I thought.
I have tried lots of different ways to draw the fields of the chess board with SDL_RenderDrawRect, this is the current state:
#include <SDL2/SDL.h>
int main()
{
bool quit = false;
SDL_Event event;
SDL_Init(SDL_INIT_EVERYTHING);
SDL_Window *window = SDL_CreateWindow("Chess", SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED, 680, 680, 0);
SDL_Renderer *render = SDL_CreateRenderer(window, -1,
SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
for (int x = 0; x > 3; x++) {
for (int y = 0; x > 8; x++) {
SDL_Rect rect;
rect.x = x*10;
rect.y = y*10;
rect.w = 128;
rect.h = 128;
SDL_SetRenderDrawColor(render, 159, 84, 8, 255);
SDL_RenderFillRect(render, &rect);
}
}
SDL_RenderPresent(render);
while (!quit) {
SDL_WaitEvent(&event);
switch (event.type) {
case SDL_QUIT:
quit = true;
break;
}
}
SDL_Quit();
return 0;
}
No rectangles want to show up when I use SDL_RenderDrawRect in a loop. Any ideas why that is?
Cheers!

There are multiple errors in your for loops.
You have to change your conditions in your for loops. Both iteration variables(x and y) start with 0, but your condition to run the foor loop is that they are greater than 3 or 8, so they wont get executed. Change it to less than the value(< instead of >)
You have to change the iteration variable of your second for loop. You create y but the condition and the value change is for x
Your Rect is 128x128 big, but you multiply your x and y from your loops by 10, that means your Rectangles will overlap. You have to multiply it by at least 128.
If you look at a Chess Board, beginning at the top left and going from left to right, every second field is colored. To implement that you have to start in the top left corner, and iterate over every cell, and then jump to the next row. Every second field, you have to draw a Rect.
This is a slightly edited version of your algorithm:
int startPos = 0;
for (int y = 0; y < 8; y++) {
for (int x = startPos; x < 3; x+=2) {
SDL_Rect rect;
rect.x = x * 129;
rect.y = y * 129;
rect.w = 128;
rect.h = 128;
SDL_SetRenderDrawColor(render, 159, 84, 8, 255);
SDL_RenderFillRect(render, &rect);
}
startPos = 1 - startPos;
}
Explanation:
In the first row, the first field will be colored, because startPos is 0.
Since every row has the opposite start of its predecessor, we have to change startPos as soon as the last cell of a row is drawn.
Since we only need to draw every second cell, the x has to be increased by two every iteration
Hope this helped you a little bit

Although ur problem is solved, leaving this for someone who face such issue
(anyhow its quite easy to make the board, I wrote this code)
this will create a standard 8x8 chess board
void Create_Board() {
SDL_Rect Box = { 0,0,80,80}; COORD position = { 0 };
bool toggle_color = 0;
for (int i = 0; i < 8; i++)
{
for (int j = 0; j < 8; j++)
{
if (toggle_color)
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
else
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
SDL_RenderFillRect(renderer, &Box);
Box.x += 80;
if(j != 7) //needed for chess color pattern
toggle_color = !toggle_color;
}
Box.x = 0;
Box.y += 80;
}
}

Related

MFC CScrollView does not clear background

My English is not perfect. I am using Visual C++ 2019 and MFC. Example program: an SDI program, the base of the view is CScrollView, draws 128*128 0s in a matrix. But MFC does not clear the background at scrolling with the scrollbar. Have you an idea? Thank you.
In settings of Windows, I am using 96 dpi * 3 = 288 dpi.
I tried: 96 dpi mode is affected so.
How can I upload the example program to this?
void CsdView::OnDraw(CDC *pDC) {
CsdDoc *pDoc = GetDocument();
ASSERT_VALID(pDoc);
if (!pDoc)
return;
CRect rect;
GetClientRect(&rect);
pDC->FillSolidRect(rect, 0xFFFFFF);
CPoint pos = GetDeviceScrollPosition();
TRACE(L"OnDraw: %4u.%4u - %4u.%4u, %4u.%4u\n", rect.right, rect.bottom, pos.x, pos.y, rect.right + pos.x, rect.bottom+pos.y);
for (int i = 0; i < 128; ++ i)
for (int j = 0; j < 128; ++ j)
pDC->TextOutW(j*20 - pos.x, i*54 - pos.y, L"0", 1);
}
void CsdView::OnInitialUpdate() {
CScrollView::OnInitialUpdate();
CSize sizeTotal;
sizeTotal.cx = 20*128;
sizeTotal.cy = 54*128;
SetScrollSizes(MM_TEXT, sizeTotal);
}
BOOL CsdView::OnEraseBkgnd(CDC *pDC) {
CBrush brush(0xFFFFFF);
FillOutsideRect(pDC, &brush);
return TRUE;
// return CScrollView::OnEraseBkgnd(pDC);
}
I can not upload picture and code as a comment, so I must edit the original question.
A little bug is remained. The orginal code (MDI MFC):
void CIDEView::OnDraw(CDC *pDC) {
CIDEDoc *const d = GetDocument();
ASSERT_VALID(d);
if (! d)
return;
CPoint const pos = GetDeviceScrollPosition();
CRect rect;
GetClientRect(&rect);
OffsetRect(&rect, pos.x, pos.y);
pDC->FillSolidRect(rect, bkcolor);
auto oldfont = pDC->SelectObject(&font);
pDC->SetBkColor(bkcolor);
pDC->SetTextColor(textcolor);
pDC->SetBkMode(TRANSPARENT);
const int cxs = pos.x / mincw, cys = pos.y / lineheight;
const int cxe = (rect.right + mincw-1) / mincw,
cye = (rect.bottom + 41) / lineheight;
for (int i = cys; i <= cye; ++ i)
for (int j = cxs; j <= cxe; ++ j)
pDC->TextOutW(textmargin+j*mincw, i*lineheight, L"0", 1);
pDC->SelectObject(oldfont);
}
void CIDEView::OnInitialUpdate() {
CScrollView::OnInitialUpdate();
SetScrollSizes(MM_TEXT, {linewidth, 128*lineheight});
}
BOOL CIDEView::OnEraseBkgnd(CDC *pDC) {
return TRUE;
}
The CScrollView class is a view with scrolling capabilities. You use it almost like a CView (ie drawing in the OnDraw() member), only you have to take into account the possible scrolling.
The GetClientRect() function returns the visible client area, and the coordinates returned are not relative to the view origin, but to the window origin, ie the left and top members are always 0. The CDC parameter in the OnDraw() function though are relative to the logical view origin, so an adjustment is needed.
As for your code, you don't need to use the OnEraseBkgnd() function, because you do so in OnDraw(). You fill only the visible part of the window, but that's very much OK. So it would best to remove it. Also, the coordinates passed to the TextOutW() function must be relative to the view origin, so the -pos.x and -pos.y adjustments are wrong. Instead, it's the rectanlge passed to the FillSolidRect() function that needs to be adjusted. So, your code would become:
void CsdView::OnDraw(CDC *pDC)
{
CsdDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
if (!pDoc)
return;
CPoint pos = GetScrollPosition();
CRect rect;
GetClientRect(&rect);
// Adjust client rect to device coordinates
OffsetRect(&rect, pos.x, pos.y);
pDC->FillSolidRect(rect, 0xFFFFFF);
for (int i = 0; i < 128; ++i)
for (int j = 0; j < 128; ++j)
pDC->TextOutW(j * 20, i * 54, L"0", 1);
}
However this code is "wasteful", as it paints the whole view. It can be optimized to paint only the visible part. It will draw only the 0s for which even one pixel lies in the visible part (didn't #define anything, just used your hard-coded 20 and 54 values). Also changed the color to yellow, so you can test it better (white is the default background color).
void CsdView::OnDraw(CDC *pDC)
{
CsdDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
if (!pDoc)
return;
CPoint pos = GetScrollPosition();
CRect rect;
GetClientRect(&rect);
// Adjust client rect to device coordinates
OffsetRect(&rect, pos.x, pos.y);
pDC->FillSolidRect(rect, 0x00FFFF);
// Paint only the items in the visible part
int xL = rect.left / 20,
xR = (rect.right + 19) / 20,
yT = rect.top / 54,
yB = (rect.bottom + 53) / 54;
for (int i = yT; i < yB; ++i)
for (int j = xL; j < xR; ++j)
pDC->TextOutW(j * 20, i * 54, L"0", 1);
}
EDIT:
In the revised code, why are the doc, pos, cxs etc variables const? There are also quite a few bugs in your logic:
You set your view size in OnInitialUpdate(), rather assuming that linewidth equals to textmargin + 128*mincw. If not, revise your code again.
The rect is already adjusted (using OffsetRect()), so it is wrong to add pos.x again.
Since you have the cell sizes in variables, don't use hard-coded numbers. For example the code for cxe should become cxe = (rect.right + mincw - 1) / mincw; Update the cye code similarly.
Also you paint at an offset of textmargin. The code should then become cxe = (rect.right - textmargin + mincw - 1) / mincw;
The code I posted works OK with the < condition in the loops, you don't need <=. Do the math and you will find that this is the correct one.

SDL2 double buffer not working, still tearing

I need a double buffer because i'm starting to notice tearing when I want to move my texture made tile map around the screen via mouse click.
I'm using SDL2 and this is a SDL2 specific question, check out my code that produces tearing, whats wrong?
//set up buffer and display
bufferTexture = SDL_CreateTexture(renderer,SDL_PIXELFORMAT_RGBA8888,SDL_TEXTUREACCESS_STREAMING,800,600);
displayTexture = SDL_CreateTexture(renderer,SDL_PIXELFORMAT_RGBA8888,SDL_TEXTUREACCESS_TARGET,800,600);
while(running)
{
handleEvents();
SDL_SetRenderTarget(renderer,bufferTexture); // everything goes into this buffer
SDL_RenderClear(renderer); // clear buffer before draw
gTileMovement.updateMapCoordinates();
for(int i = 0; i < MAP_ROWS; i++)//rows
{
for(int j = 0; j < MAP_COLUMNS; j++)//columns
{
x = (j * 100) - (i * 100);
y = ((i * 100) + (j * 100)) / 2;
drawTiles(i,j,x,y);
}
}
//move from buffer to display texture
memcpy(&displayTexture,&bufferTexture,sizeof((&bufferTexture)+1));
//change render target back to display texture
SDL_SetRenderTarget(renderer,displayTexture);
//show it all on screen
SDL_RenderPresent(renderer);
}
for all it matters here is my drawTiles function too, is this not conventional? :
void drawTiles(int i,int j,int x,int y)
{
//updates based on a mouse clicks xy coords
gTileMovement.updateMapCoordinates();
if(tileMap[i][j] == 1) // grass?
{
gSpriteSheetTexture.render(x+gTileMovement.getUpdatedX(),y+gTileMovement.getUpdatedY(),&gSpriteClips[1]);
}
if(tileMap[i][j] == 0) // wall?
{
gSpriteSheetTexture.render(x+gTileMovement.getUpdatedX(),y+gTileMovement.getUpdatedY(),&gSpriteClips[0]);
}
if(tileMap[i][j] == 2) // tree?
{
gSpriteSheetTexture.render(x+gTileMovement.getUpdatedX(),y+gTileMovement.getUpdatedY(),&gSpriteClips[2]);
}
}
Which followes into how I SDL_RenderCopy the tiles through a class. This copies the textures onto the current targeted renderer does it not? Which is the buffer texture if i'm not mistaken.
void LTexture::render(int x, int y, SDL_Rect * clip)
{
SDL_Rect renderQuad = {x, y, mWidth, mHeight};
if(clip != NULL)
{
renderQuad.w = clip->w;
renderQuad.h = clip->h;
}
SDL_RenderCopy(renderer, mTexture, clip, &renderQuad);
}

Better control of my client area(WIN32)

I am trying to make a 3 x 3 grid with a black pen on a window. however I want it to be centered, for example that my grid is inside a white space,
10% of top, right, left and bottom. and my grid will fit in the remaining 80% even when we resize the window.
Now I could make the grid but after several attempts to create the 10% area, got frustrated.
case WM_SIZE:
//get the 10% range.
cxInvalid = LOWORD(lParam) * 0.1;
cyInvalid = HIWORD(lParam) * 0.1;
//get the grid, DIVISIONS = 3
cxBlock = LOWORD(lParam) / DIVISIONS;
cyBlock = HIWORD(lParam) / DIVISIONS;
return 0;
Thanks in advaced :)
case WM_PAINT:
hdc = BeginPaint(hwnd, &ps);
for (x = 0; x < DIVISIONS; x++)
for (y = 0; y < DIVISIONS; y++)
{
Rectangle(hdc, x * cxBlock, y * cyBlock,
(x + 1) * cxBlock, (y + 1) * cyBlock);
}
EndPaint(hwnd, &ps);
return 0;
This is exactly the sort of problem for which Windows mapping modes are intended to be used. For the moment I'm going to assume that you want your grid to remain square, regardless of the shape of the window it's in.
One way to do that is to switch from the default MM_TEXT mapping mode to the MM_ISOTROPIC mapping mode (but if we want the grid to change shape with the surrounding window, we'd use MM_ANISOTRCOPIC instead).
Using that, we can set our window as a virtual grid of, say, 1200 x 1200 cells, and then draw our 3x3 grid on that. I've chosen 1200 x 1200 so the part we care about will be a nice, convenient 1000 x 1000 grid.
// set up the mapping mode:
RECT rect;
GetClientRect(hWnd, &rect);
SetMapMode(hDC, MM_ISOTROPIC);
SetViewportExt(rect.x, rect.y);
// The virtual width/height for our window:
static const int width = 1200;
static const int height = 1200;
SetWindowExt(width, height);
SetWindowOrg(-100, -100); // Set the virtual 0 point ~10% of the way into the window.
// And then draw the grid. We always draw in a 1000 x 1000 grid, and Windows
// scales that to the actual window size for us.
//
static const int grid_size = 1000;
static const int step = grid_size / 3;
for (int i = step; i < grid_size-1; i += step) {
MoveTo(hDC, i, 0);
LineTo(hDC, i, grid_size);
MoveTo(hDC, 0, i);
LineTo(hDC, grid_size, i);
}
To reiterate the difference between MM_ISOTROPIC and MM_ANISOTROPIC, here are screen shots of the grid. First as it's drawn with MM_ISOTROPIC:
...and then as it's drawn with MM_ANISOTROPIC:

SDL How to animate a Sprite when key is pressed

I'm going to get straight to the point:
I have a sprite sheet that has 8 movement sprites (http://i.imgur.com/hLpo2Qn.png cant post images :\ ), so it creates the illusion that im walking)
My problem is that when i press and hold a key i want the sprite to kinda have an animated walk. For example when i press the up arrow the first two sprites play in a loop until i let go of the key. All I've been able to do so far is to get only one sprite showing for each direction, so it kinda gives an unrealistic effect.
Code:
#include <SDL/SDL.h>
int main(int argc, char* argv[])
{
SDL_Init(SDL_INIT_EVERYTHING);
SDL_Surface* screen, *image;
screen = SDL_SetVideoMode(640, 480, 32, SDL_SWSURFACE);
bool running = true;
const int FPS = 30;
Uint32 start;
bool direction[4] = {0, 0, 0, 0};
SDL_Rect pos;
pos.x = 0;
pos.y = 0;
SDL_Rect sprite;
sprite.x = 160;
sprite.y = 0;
sprite.w = 32;
sprite.h = 32;;
Uint32 color = SDL_MapRGB(screen -> format, 0xff, 0xff, 0xff);
image = SDL_DisplayFormat(SDL_LoadBMP("sprite.bmp"));
SDL_SetColorKey(image, SDL_SRCCOLORKEY, SDL_MapRGB(screen -> format, 255, 0, 255));
while (running == true)
{
start = SDL_GetTicks();
SDL_Event event;
while(SDL_PollEvent(&event))
{
switch(event.type)
{
case SDL_QUIT:
running = false;
break;
case SDL_KEYDOWN:
switch(event.key.keysym.sym)
{
case SDLK_UP:
direction[0] = 1;
sprite.x = 0;
break;
case SDLK_LEFT:
direction[1] = 1;
sprite.x = 224;
break;
case SDLK_DOWN:
direction[2] = 1;
sprite.x = 160;
break;
case SDLK_RIGHT:
direction[3] = 1;
sprite.x = 96;
break;
}
break;
case SDL_KEYUP:
switch(event.key.keysym.sym)
{
case SDLK_UP:
direction[0] = 0;
sprite.x = 32;
break;
case SDLK_LEFT:
direction[1] = 0;
break;
case SDLK_DOWN:
direction[2] = 0;
break;
case SDLK_RIGHT:
direction[3] = 0;
break;
}
break;
}
}
if(direction[0])
pos.y-- ;
if(direction[1])
pos.x--;
if(direction[2])
pos.y++;
if(direction[3])
pos.x++;
SDL_FillRect(screen, &screen -> clip_rect, color);
SDL_BlitSurface(image, &sprite, screen, &pos);
SDL_Flip(screen);
if(1000 / FPS > SDL_GetTicks() - start)
SDL_Delay(1000 / FPS - (SDL_GetTicks() - start));
}
SDL_FreeSurface(image);
SDL_Quit();
return 0;
}
Disclaimer: This is not the only or best way to achieve what you wanting. It is simply one possible solution
You need to implement a basic timer/counter to switch between the two appropriate images.
For this example I would store your sprite offsets in a 2D array int sprite_x[4][2]; - this would represent the four directions and the two image offsets for each. Then on a key press instead of using a bool array just have a single integer index int sprite_index; for indexing the sprite offsets. All you would then need to do is have another integer value called something like int current_key; which you would switch between 0 and 1.
// Used for indexing the 2D array
enum
{
WALK_LEFT,
WALK_RIGHT,
WALK_UP,
WALK_DOWN
WALK_MAX
};
// How many sprites per animation
const int NUM_KEYFRAMES = 2;
// 2D array to hold the x value offsets for sprites
int sprite_x[WALK_MAX][NUM_KEYFRAMES];
int sprite_index = WALK_LEFT; // Current animation
int current_key = 0; // Current keyframe for the animation
// Set up all the x value offsets in the array
sprite_x[WALK_LEFT][0] = 0;
sprite_x[WALK_LEFT][1] = 32;
sprite_x[WALK_RIGHT][0] = 64;
sprite_x[WALK_RIGHT][1] = 96;
.
.
.
You then simply update the sprite x value accordingly sprite.x = sprite_x[sprite_index][current_key];

SDL_surface that contains several images

Suppose I have a SDL_Surface that is just one image.
What if I wanted to make that SDL_Surface have three copies of that image, one below the other?
I came up with this function, but it doesn't show anything:
void ElementView::adjust()
{
int imageHeight = this->img->h;
int desiredHeight = 3*imageHeight;
int repetitions = desiredHeight / imageHeight ;
int remainder = desiredHeight % imageHeight ;
SDL_Surface* newSurf = SDL_CreateRGBSurface(img->flags, img->w, desiredHeight, 32, img->format->Rmask, img->format->Gmask, img->format->Bmask,img->format->Amask);
SDL_Rect rect;
memset(&rect, 0, sizeof(SDL_Rect));
rect.w = this->img->w;
rect.h = this->img->h;
for (int i = 0 ; i < repetitions ; i++)
{
rect.y = i*imageHeight;
SDL_BlitSurface(img,NULL,newSurf,&rect);
}
rect.y += remainder;
SDL_BlitSurface(this->img,NULL,newSurf,&rect);
if (newSurf != NULL) {
SDL_FreeSurface(this->img);
this->img = newSurf;
}
}
I think you should
Create a new surface that is 3 times as long as the initial one
Copy from img to the new surface using code similar to what you have (SDL_BlitSurface), except having the destination as your new surface
SDL_FreeSurface on your original img
Assign your new surface to img
Edit: Here is some sample code, didn't have time to test it though...
void adjust(SDL_Surface** img)
{
SDL_PixelFormat *fmt = (*img)->format;
SDL_Surface* newSurf = SDL_CreateRGBSurface((*img)->flags, (*img)->w, (*img)->h * 3, fmt->BytesPerPixel * 8, fmt->Rmask, fmt->Gmask, fmt->Bmask, fmt->Amask);
SDL_Rect rect;
memset(&rect, 0, sizeof(SDL_Rect));
rect.w = (*img)->w;
rect.h = (*img)->h;
int i = 0;
for (i ; i < 3; i++)
{
SDL_BlitSurface(*img,NULL,newSurf,&rect);
rect.y += (*img)->h;
}
SDL_FreeSurface(*img);
*img = newSurf;
}