0
\$\begingroup\$

right at the moment i am writting some code for demonstration purposes. unfortunatly i have an stubborn bug in my code which is invisible to me. The Problem is I am trying to draw a Map. The Map contains information of the tiles which should be visible to the user. So far so good. The Map I use is just an 2D array of Ints, hardcoded into the application. I Translate then this 2D array of ints into an 1D-array which then just stores any visible tile on the screen.

It does somehow work.enter image description here

The way i am drawing recognize the different tiles. But around the red block of tiles there should orange tiles around it but as you see you see nothing. And this is what freaks me out. the clipping should just work fine but apparently it does not. I cannot figure out why. debugger does not help here unfortunalty.

I would appreciate any help which points out the bug i have here. probably something very stupid.

Here is my code ( https://github.com/Johanneslueke/C---Projekt-AEIT6/blob/master/C%20-%20Projekt%20AEIT6/Tiles.c )

#include "Introduction.h"

#define TILE_WIDTH 50
#define TILE_HEIGHT 50
#define TILE_TYPE_RED 0
#define TILE_TYPE_BLUE 1
#define TILE_TYPE_GREEN 2
#define TILE_TYPE_ORANGE 3
#define TILE_TYPE_NONE 4

#define TILE_MAP_WIDTH 10
#define TILE_MAP_HEIGHT 10

typedef struct {
SDL_Rect Outlines;
int      Type;
}Tile;

Tile TileClips[4];
SDL_Texture* TileTex = NULL;

Tile initTile(int x, int y, int Type);
SDL_Rect getTileOutlines(Tile* tile);
int getTypeOfTile(Tile* tile);

void render(int x, int y, SDL_Texture* tex, SDL_Rect* clip, SDL_Renderer* renderer)
{

   //Set rendering space and render to screen
   SDL_Rect renderQuad = { x, y, 0, 0 };


   //Set clip rendering dimensions
   if (clip != NULL)
   {
        renderQuad.w = clip->w;
        renderQuad.h = clip->h;
   }
    else abort();;

    //Render to screen       /--------/ /TargetAreaOnScreen/
    SDL_RenderCopy(renderer, tex, clip, &renderQuad);
 }

void RenderTileMap(Tile* Tiles, SDL_Texture* TileSheet, SDL_Rect* TileClips, SDL_Renderer* renderer) {

    for (int i = 0; i < TILE_MAP_WIDTH*TILE_MAP_HEIGHT; i++)
    {
        int x = getTileOutlines(&Tiles[i]).x; //Position on the Screen
        int y = getTileOutlines(&Tiles[i]).y;
        int type = getTypeOfTile(&Tiles[i]);
        SDL_Rect clip = TileClips[type]; // Position of the tile on the TileSheet

        render( x, //Position of the tile
            y, //
            TileSheet,                    //Source of the Tile
            &clip,
        renderer);
    }


    }

int main(int argc, char* argv[]) {


    TileClips[TILE_TYPE_BLUE].Outlines.h = TILE_HEIGHT;
    TileClips[TILE_TYPE_BLUE].Outlines.w = TILE_WIDTH;
    TileClips[TILE_TYPE_BLUE].Outlines.x = 0;
    TileClips[TILE_TYPE_BLUE].Outlines.y = 0;

    TileClips[TILE_TYPE_RED].Outlines.h = TILE_HEIGHT;
    TileClips[TILE_TYPE_RED].Outlines.w = TILE_WIDTH;
    TileClips[TILE_TYPE_RED].Outlines.x = 50;
    TileClips[TILE_TYPE_RED].Outlines.y = 0;

    TileClips[TILE_TYPE_GREEN].Outlines.h = TILE_HEIGHT;
    TileClips[TILE_TYPE_GREEN].Outlines.w = TILE_WIDTH;
    TileClips[TILE_TYPE_GREEN].Outlines.x = 0;
    TileClips[TILE_TYPE_GREEN].Outlines.y = 50;

    TileClips[TILE_TYPE_ORANGE].Outlines.h = TILE_HEIGHT;
    TileClips[TILE_TYPE_ORANGE].Outlines.w = TILE_WIDTH;
    TileClips[TILE_TYPE_ORANGE].Outlines.x = 50;
    TileClips[TILE_TYPE_ORANGE].Outlines.y = 50;

    int TileMap[TILE_MAP_WIDTH][TILE_MAP_HEIGHT] = {
    { TILE_TYPE_ORANGE ,TILE_TYPE_ORANGE ,TILE_TYPE_ORANGE ,TILE_TYPE_ORANGE ,TILE_TYPE_ORANGE ,TILE_TYPE_ORANGE ,TILE_TYPE_ORANGE ,TILE_TYPE_ORANGE ,TILE_TYPE_ORANGE ,TILE_TYPE_ORANGE },//0
    { TILE_TYPE_ORANGE ,TILE_TYPE_RED, TILE_TYPE_RED, TILE_TYPE_RED, TILE_TYPE_RED, TILE_TYPE_RED, TILE_TYPE_RED, TILE_TYPE_RED, TILE_TYPE_RED, TILE_TYPE_ORANGE },//1
    { TILE_TYPE_ORANGE ,TILE_TYPE_RED, TILE_TYPE_RED, TILE_TYPE_RED, TILE_TYPE_RED, TILE_TYPE_RED, TILE_TYPE_RED, TILE_TYPE_RED, TILE_TYPE_RED, TILE_TYPE_ORANGE },//3
    { TILE_TYPE_ORANGE ,TILE_TYPE_RED, TILE_TYPE_RED, TILE_TYPE_RED, TILE_TYPE_BLUE, TILE_TYPE_RED, TILE_TYPE_RED, TILE_TYPE_RED, TILE_TYPE_RED, TILE_TYPE_ORANGE },//4
    { TILE_TYPE_ORANGE ,TILE_TYPE_RED, TILE_TYPE_RED, TILE_TYPE_RED, TILE_TYPE_BLUE, TILE_TYPE_RED, TILE_TYPE_RED, TILE_TYPE_RED, TILE_TYPE_RED, TILE_TYPE_ORANGE },//5
    { TILE_TYPE_ORANGE ,TILE_TYPE_RED, TILE_TYPE_RED, TILE_TYPE_RED, TILE_TYPE_BLUE, TILE_TYPE_RED, TILE_TYPE_RED, TILE_TYPE_RED, TILE_TYPE_RED, TILE_TYPE_ORANGE },//6
    { TILE_TYPE_ORANGE ,TILE_TYPE_RED, TILE_TYPE_RED, TILE_TYPE_GREEN, TILE_TYPE_RED, TILE_TYPE_RED, TILE_TYPE_RED, TILE_TYPE_RED, TILE_TYPE_RED, TILE_TYPE_ORANGE },//7
    { TILE_TYPE_ORANGE ,TILE_TYPE_RED, TILE_TYPE_RED, TILE_TYPE_RED, TILE_TYPE_RED, TILE_TYPE_RED, TILE_TYPE_RED, TILE_TYPE_RED, TILE_TYPE_RED, TILE_TYPE_ORANGE },//8
    { TILE_TYPE_ORANGE ,TILE_TYPE_ORANGE ,TILE_TYPE_ORANGE ,TILE_TYPE_ORANGE ,TILE_TYPE_ORANGE ,TILE_TYPE_ORANGE ,TILE_TYPE_ORANGE ,TILE_TYPE_ORANGE ,TILE_TYPE_ORANGE ,TILE_TYPE_ORANGE },//9
};

Tile TileArray[TILE_MAP_WIDTH*TILE_MAP_HEIGHT];
for (int i = 0,k=0; i < TILE_MAP_WIDTH; i++)
    for (int j = 0; j < TILE_MAP_HEIGHT; j++)
    {
        switch (TileMap[i][j]) {
        case TILE_TYPE_BLUE:
            TileArray[k++] = initTile(j*TILE_WIDTH, i*TILE_HEIGHT, TILE_TYPE_BLUE);
            break;
        case TILE_TYPE_RED:
            TileArray[k++] = initTile(j*TILE_WIDTH, i*TILE_HEIGHT, TILE_TYPE_RED);
            break;
        case TILE_TYPE_GREEN:
            TileArray[k++] = initTile(j*TILE_WIDTH, i*TILE_HEIGHT, TILE_TYPE_GREEN);
            break;
        case TILE_TYPE_ORANGE:
            TileArray[k++] = initTile(j*TILE_WIDTH, i*TILE_HEIGHT, TILE_TYPE_ORANGE);
            break;
        case TILE_TYPE_NONE:
            TileArray[k++] = initTile(j*TILE_WIDTH, i*TILE_HEIGHT, TILE_TYPE_NONE);
            break;
        }


    }



if (!init()) {

}

TileTex = loadImage("tiles.bmp", gRenderer);
while (!quit)
{
    SDL_Event e;
    //Handle events on queue
    while (SDL_PollEvent(&e) != 0)
    {
        //User requests quit
        if (e.type == SDL_QUIT)
        {
            quit = TRUE;
        }
    }

    //Clear screen
    SDL_RenderClear(gRenderer);

    //Render texture to screen
    //SDL_RenderCopy(gRenderer, Tex, NULL, NULL);
    RenderTileMap(TileArray, TileTex, TileClips, gRenderer);

    //Update screen
    SDL_RenderPresent(gRenderer);
}

return 0;

}


Tile initTile(int x, int y, int Type) {
Tile t;
t.Outlines.x = x;
t.Outlines.y = y;
t.Outlines.w = TILE_WIDTH;
t.Outlines.h = TILE_HEIGHT;
t.Type = Type;
return t;
}

SDL_Rect getTileOutlines(Tile* tile) {
return tile->Outlines;
}
int getTypeOfTile(Tile* tile) {
return tile->Type;
}
\$\endgroup\$
1
\$\begingroup\$

So, I have found my bug....

I accidently used my own defined struct "Tiles" to store the data for the clipping. because how C layout structs in memory i could use the struct Tiles as argument for my renderfunction which expectet an pointer of type SDL_Rect. Nasty little bug...

on my respository on github you can find the corrected version of the code!!!

\$\endgroup\$
  • \$\begingroup\$ This sounds like something that should never have compiled in the first place... Always -Wall -Wextra -Wfatal-errors -pedantic-errors. \$\endgroup\$ – Quentin Mar 5 '18 at 10:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.