Question

I have been working on this game using SDL2.0 and C++ and have hit a snag. I have created the move function and am calling the right rectangle. And have called the move function in the main loop and it just doesn't move.

Here is the code.

//
//  Window.cpp
//  Galaxy Shooter
//
//  Created by Samrith on 02/03/14.
//  Copyright (c) 2014 Sam Shankar. All rights reserved.
//

#include "Window.h"

void Window::create() {
    Resources r;

    SDL_Init(SDL_INIT_EVERYTHING);

    window = SDL_CreateWindow("Galaxy Shooter", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
    screen = SDL_GetWindowSurface(window);

    if(window == NULL)
        std::cout << " Window cannot be created. Error: " << SDL_GetError();

    else {
        r.load();
        SDL_UpdateWindowSurface(window);
    }
}

void Window::destroy() {
    Resources r;
    SDL_DestroyWindow(window);
    r.free();
    SDL_Quit();
}

void Window::run() {
    bool done = false;
    Resources r;
    create();
    while(!done)
    {
        while(SDL_PollEvent(&event) != 0)
        {
            if(event.type == SDL_QUIT)
            {
                done = true;
                destroy();
            }
            r.move(event); //This is the move function
            SDL_UpdateWindowSurface(window);
        }
        r.projectile();
        SDL_UpdateWindowSurface(window);
    }
}

SDL_Window * Window::getWindow() {
    return window;
}

SDL_Surface * Window::getSurface() {
    return screen;
}

And here is the resources source file which contains the move function:

//
//  Resources.cpp
//  Galaxy Shooter
//
//  Created by Samrith on 06/03/14.
//  Copyright (c) 2014 Sam Shankar. All rights reserved.
//

#include "Resources.h"

bool b[4] = {0, 0, 0, 0};

void Resources::load() {

    background();
    rocket();
    //r.create();
}

void Resources::free() {
    Sound s;

    SDL_FreeSurface(bgimage);
    SDL_FreeSurface(rimage);
    //SDL_FreeSurface(screen);
    s.destroy();
}

//void Resources::run() {
//    Window w;
//    w.run();
//}

void Resources::background() {
    Window w;
    Sound s;

    //Load screen
    screen = SDL_GetWindowSurface(w.getWindow());

    if(screen == NULL)
        std::cout << "Surface (background) error: " << SDL_GetError();

    //Load image
    bgimage = IMG_Load("bg2.jpg");

    if(bgimage == NULL)
        std::cout << "Image (background) error: " << SDL_GetError();

    //Load rectangle and set values
    bgrect.x = 0;
    bgrect.y = 0;
    bgrect.h = w.screenH();
    bgrect.w = w.screenW();

    //Blit image to screen
    SDL_BlitScaled(bgimage, NULL, screen, &bgrect);
    s.init(1);
}

void Resources::rocket() {
    Window w;
    Sound s;

    //Load screen
    screen = SDL_GetWindowSurface(w.getWindow());

    if(screen == NULL)
        std::cout << "Surface (rocket) error: " << SDL_GetError();

    //Load image
    rimage = IMG_Load("rocket.png");

    if(rimage == NULL)
        std::cout << "Image (rocket) error: " << SDL_GetError();

    //Load rectangle and set values
    rrect.x = 15;
    rrect.y = w.screenH()/2;
    rrect.h = 75;
    rrect.w = 75;

    //Blit image to screen
    SDL_BlitScaled(rimage, NULL, screen, &rrect);
    //s.init(2);
}

void Resources::move(SDL_Event event) {
    //bool done = false;
    Window w;

    switch(event.type)
    {
        case SDL_KEYDOWN:
            switch(event.key.keysym.sym)
            {
                case SDLK_UP:
                    b[0] = 1; break;

                case SDLK_DOWN:
                    b[1] = 1; break;

                case SDLK_RIGHT:
                    b[2] = 1; break;

                case SDLK_LEFT:
                    b[3] = 1; break;
            }
            break;

        case SDL_KEYUP:
            switch(event.key.keysym.sym)
            {
                case SDLK_UP:
                    b[0] = 0; break;

                case SDLK_DOWN:
                    b[1] = 0; break;

                case SDLK_RIGHT:
                    b[2] = 0; break;

                case SDLK_LEFT:
                    b[3] = 0; break;
            }
            break;
    }

    if(b[0]==true)
        rrect.y += 10;
    if(b[1]==true)
        rrect.y -= 10;
    if(b[2]==true)
        rrect.x += 10;
    if(b[3]==true)
        rrect.x -= 10;
}

void Resources::projectile() {
    Window w;
    SDL_PumpEvents();
    int x, y;
    SDL_GetMouseState(&x, &y);
    if(SDL_GetMouseState(&x, &y) & SDL_BUTTON(1))
    {
        rrect.x = x;
        rrect.y = y;
        SDL_BlitScaled(rimage, NULL, screen, &rrect);
        for(int i=rrect.x; i<=w.screenW(); i++)
            rrect.x+=2;

    }
}

Also, I have Googled this problem and tried out various tutorials and docs. This is my last resort and not the first place I turned to for this problem. So please be kind. :)

Was it helpful?

Solution

by declaring Resource r in every function you create a new instance of the Resource class every time thus creating many different Resource objects.

The same goes for the Window w variable you declare in every function of the class Resource: each function call will create a new window.

I suggest you to read about Object Oriented Programming and variable scoping.

Also, C++ is not the most friendly language to start learning about Object Oriented Programming.

If you are starting out, I suggest you to switch to a simpler language such as Java or C#.

Good luck.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top