0

I'm trying to write a small game where boxes drop down from the top of the window. But for some reason, I can't change a internal variable in the class, the y-coordinate. I don' knowif I'm missing something basic, but I can't find the bug.

Box.h

#pragma once
#include "SDL.h"
class Box
{
public:
    Box();
    ~Box();
    void setX (int a);
    void setY (int a);
    void setSpeed (int a);
    void setSurface ();
    void render(SDL_Surface *source, SDL_Window *win);
    void update();


private:
    int x;
    int y;
    int speed;
    SDL_Surface *sur;
    SDL_Rect rect;
};

Box.cpp

#include "Box.h"
#include "SDL_image.h"

#include <iostream>

void Box::setX(int a)
{
    x = a;
}

void Box::setY (int a)
{
    y = a;
}

void Box::setSpeed (int a)
{
    speed = a;
}

void Box::setSurface()
{
    sur = IMG_Load("C:/hello.bmp");
    if (sur == NULL)
    {
        std::cout << IMG_GetError();
    }
}

Box::Box()
{
    speed = 5;
    y = 0;
    x = 3;
    rect.x = 0;
    rect.y = 0;
}

Box::~Box()
{

}

void Box::render(SDL_Surface *source, SDL_Window *win)
{
    SDL_BlitSurface(sur, NULL, source, &rect);
    SDL_UpdateWindowSurface(win);   
}

void Box::update()
{
    setY(y + speed); //I've also tried y = y + speed
    rect.y = y;
}

main.cpp

#include "SDL.h"
#include "Box.h"
#include "SDL_image.h"
#include <iostream>

bool init();
void update(Box test);
void render(Box test);
SDL_Window *win;
SDL_Surface *source;

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

    bool quit = false;
    SDL_Event e;
    Box test;
    test.setSurface();
    test.render(source, win);

    while (quit ==false)
    {
        while( SDL_PollEvent( &e ) != 0 )
        {    
            if( e.type == SDL_QUIT )
            {
                quit = true;
            }

        }
        update(test);
        render(test);


    }
    return 0;
}
void update(Box test)
{   
    test.update();
}
void render(Box test)
{
    test.render(source, win);
}
bool init()
{
    if (SDL_Init(SDL_INIT_EVERYTHING) != 0)
    {
        std::cout << "SDL_Init Error: " << SDL_GetError() << std::endl;
        return 1;
    }

    win = SDL_CreateWindow("Hello World!", 100, 100, 640, 480, SDL_WINDOW_SHOWN);

    if (win == NULL)
    {
        std::cout << "SDL_CreateWindow Error: " << SDL_GetError() << std::endl;
        return 1;
    }
    source = SDL_GetWindowSurface(win);
    return true;
}
Pontus
  • 13
  • 1
  • You don't need to show us your entire program. Only show the code that is relevant. Give us a minimal example that we can take and compile ourselves in order to get the same error. – David G Jan 29 '14 at 23:25
  • Haha, sorry. I guess that was a bit overkill. – Pontus Jan 29 '14 at 23:30

1 Answers1

3

update takes its Box argument by value, so a copy of the original Box is always made when update(test) is called. This copy is then modified, and the original is left unchanged. To fix this, make update take its argument by reference.

void update(Box& test);

void update(Box& test)
{   
    test.update();
}
Brian Bi
  • 111,498
  • 10
  • 176
  • 312