0

Having a hard time trying to wrap my head around why my grid system fails, I'm assuming my train of thought is stuck at the moment(akin to writers block). My understanding of grids is while the current cell isn't the last one in the column/row(w/e is horizontal) /do stuff/ then increment to the next, else if its the last cell then go to the next "line"(row/column(w/e is vertical)). I guess what I'm asking is what is the problem with my logic and or code? and is there a std::container better suited for this task? (also sorry if this is the wrong forum but the header in C++ said to ask all game development questions here)

I've tried several methods if you look through my github history at one point it was "working" though I've since started a rewrite of everything since my old code base was ugly to look at.

bottom is the grid header its mostly agnostic but does depend on sdl2
rest of the code is at My github repo
it does include a Makefile(for linux users) for windows users you only have to tell your compiler to search src/ for includes and link against SDL2 SDL2_image, (the drop the physics header if you don't want to link against box2d same with the filesystem one(cept tinyxml in this case)

        #ifndef __GRID_HPP__
        #define __GRID_HPP__
        #include <common.hpp>
        #include <gfx/gfx_common.hpp>

        //the *getSize()'s you see returns a pointer to an SDL_Rect which is a struct
        //containing 4 members (h,w,x,y).
        //*getImage()'s just returns an SDL_Texture *
        //this is also templated so that it will be class agnostic

        template <typename Element>
        class Grid
        {
            private:
            protected:
                std::vector<Element> grid;
                int padding,gx,gy;
                SDL_Rect position;
            public:
                Grid(int x = 0, int y = 0, int p = 50, int gw = 3, int gh = 3)
                {
                    padding = p;
                    position.x = x; //this is the position of the first element
                    position.y = y;
                    gx = gw;
                    gy = gh;
                }
                void gridify()
                {
                    typename std::vector<Element>::iterator i;
                    SDL_Rect * current = grid.begin()->getSize();
                    SDL_Rect * last = current;
                    int count = 0, row = 0;
                    for(i = grid.begin(); i != grid.end(); i++)
                    {
                       if(i == grid.begin())
                       {
        #ifdef DEBUG
                           printf("First Element\n");
                           printf("X: %d Y: %d\n", i->getSize()->x, i->getSize()->y);
        #endif
                           i->getSize()->x = position.x;
                           i->getSize()->y = position.y;
        #ifdef DEBUG
                           printf("After Assignment(First Element)\n");
                           printf("X: %d Y: %d\n", i->getSize()->x, i->getSize()->y);
        #endif
                           last = i->getSize();
                       }
                       else
                       {
                            if(count != gx)
                            {
        #ifdef DEBUG
                                printf("Before assign X: %d\n",i->getSize()->x);
        #endif
                                i->getSize()->x = last->x + padding;
        #ifdef DEBUG
                                printf("After assign X: %d\n",i->getSize()->x);
        #endif
                                count++;
                            }
                            else
                            {
                                count = 0;
                                row++;
                                i->getSize()->x = position.x;
                                i->getSize()->y = last->x + padding;
                            }
                       }
                    }
                }
                void update(SDL_Renderer * rnd)
                {
                    typename std::vector<Element>::iterator i;
                    for(i = grid.begin(); i != grid.end(); i++)
                    {
                    SDL_RenderCopy(rnd, i->getImage(), NULL, i->getSize());
                    }
                }
                void append(Element& element)
                {
                    grid.push_back(element);
                }
        };
        #endif // __GRID_HPP__

Edited by Xecantur: guess the code box for scrolling is gone :(

1
Contributor
1
Reply
12
Views
3 Years
Discussion Span
Last Post by Xecantur
0

Update: its mostly working now, (github page has the latest), the Y value keeps getting reset to 0 though somewhere in the loop.
Update 2: WOOT works now had to set Y again in that if(count != (gx - 1) block with i->getSize()->y = last->y; !! :P

Edited by Xecantur: Fixed my problem

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.