mercredi 2 décembre 2015

Why does my program crash when usind SDL_RenderFillRect()?

i am trying to make a program that draws to the screen, while sorting an array, like in this vid: https://www.youtube.com/watch?v=kPRA0W1kECg . My problem is: i overloaded the comparison and assign operator, and in the assign operator i want to call a draw() function, which will draw to the screen. But when i do this, my code fails, to be exact the line SDL_RenderFillRect(renderer, &r) which causes it to crash. (it starts running then crashes, cant even end in task manager). My code:

    using namespace std;
int main(int argc, char *argv[])
{
    srand(time(NULL));

SDL_Window *window = nullptr;
SDL_Renderer *renderer = nullptr;
window = SDL_CreateWindow("Rendezo algoritmus", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 0, 0, SDL_WINDOW_FULLSCREEN_DESKTOP);
renderer = SDL_CreateRenderer(window, 0, 0);
SDL_SetRenderDrawColor(renderer, 100, 100, 100, 255);
SDL_RenderClear(renderer);

SDL_Rect r;
r.y = 0;
r.x = 0;
r.w = 50;
r.h = 50;


SortHelper<int> mySortInts[] = { SortHelper<int>{2}, SortHelper<int>{5}, SortHelper<int>{1}, SortHelper<int>{4}, SortHelper<int>{3} };
std::vector<SortHelper<int>> myvector(mySortInts, mySortInts + 5);

SDL_Rect *rectArr = new SDL_Rect[myvector.size()];
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
int i = 0;
int rectXCoord = 0;
int rectWidth = (SDL_GetWindowSurface(window)->w) / myvector.size();
for (SortHelper<int> s : myvector) {
    s.setRenderer(renderer);
    rectArr[i].w = rectWidth;
    rectArr[i].h = s.getHeight();
    rectArr[i].x = rectXCoord;
    rectArr[i].y = (SDL_GetWindowSurface(window)->h) - (rectArr[i].h);
    rectXCoord += (SDL_GetWindowSurface(window)->w) / myvector.size();
    SDL_RenderFillRect(renderer, &rectArr[i]);
    SDL_RenderPresent(renderer);
}
std::sort(myvector.begin(), myvector.end());
SDL_Delay(500);

SDL_DestroyWindow(window);
SDL_Quit();

delete[] rectArr;
return EXIT_SUCCESS;}

And my sorthelper.h:

#pragma once
class SDL_Renderer;
template<typename T>
class SortHelper {
T data;
int height;
int width;
int x;
int y;
SDL_Renderer *renderer;
public:
SortHelper(SortHelper &a) { data = a.data; }
SortHelper(T a) { data = a; }
bool operator<(SortHelper a) { 
return data < a.data;
}
void operator=(SortHelper const a) { 
    data = a.data;
    height = a.height;
    draw();
    SDL_Delay(10);
}
T getValue() { return data; }
int getHeight() {

    return rand() % 500;
}
void setRenderer(SDL_Renderer *r) {
    renderer = r;
}
void draw() {       
    SDL_Rect r;
    r.x = x;
    r.y = y;
    r.w = width;
    r.h = height;
    SDL_RenderFillRect(renderer, &r);
    SDL_RenderPresent(renderer);
}};

Now i might have an idea what causes the problem, but can not come up nor find any solution for it. I think what ruins the code is the SDL_Renderer class declaration at the start of sorthelper.h. But if i remove that declaration, my code won't compile giving syntax error where i declare SDL_Renderer *renderer;. So if i am right, my question is, how can i declare an SDL_Renderer member variable inside a class? If I am not, then what is the problem?

Aucun commentaire:

Enregistrer un commentaire