1

Hi there I'm learning c++ so I'm not sure if this is a stupid mistake on my part. I'm trying to do something simple with raylib, I'm trying to simply clear the background by calling a method in a class. When I try to do this from the class, the window flickers horribly and sometimes freezes. But when it's called inside main it runs perfectly fine and acts like expected. I've programmed in lots of other languages before and I'm not quite sure why it should behave differently.

Here's the code:
main.cpp:

#include <iostream>
#include <string>
#include <array>
#include "Viewer.h"
#include <raylib.h>

using namespace std;

#define log(x) std::cout << x << std::endl


int main() {
    int screenWidth = 800;
    int screenHeight = 450;

    InitWindow(screenWidth, screenHeight, "test");

    Viewer* viewer = new Viewer();

    while (!WindowShouldClose()) {
        BeginDrawing();
        //ClearBackground(DARKGRAY);
        //DrawText("Congrats!", 190, 200, 20, LIGHTGRAY);
        //DrawLine

        viewer->draw();
        EndDrawing();
    }

    delete viewer;
    CloseWindow();

    //cin.get();
}

viewer.cpp

#include <raylib.h>

class Viewer {
public:
    void draw() {
        ClearBackground(DARKGRAY);

    }
};

viewer.h

#pragma once

class Viewer {
public:
    void draw(){}
};

Extra info, I'm using Visual Studio 2019 and vckpg to manage libraries.

This is what I'm seeing: enter image description here

g1i1ch
  • 85
  • 1
  • 1
  • 4

1 Answers1

3

It looks like you're re-defining your draw function. So it's most likely calling the empty draw function definition in your header.

viewer.h

void draw(){} //This is most likely what is being called.
void draw(); //Use this if you want to define it in .cpp

Either you can keep the .cpp definition and go with void draw();. Or you can just call the clear in the header viewer.h

void draw(){ ClearBackground(DARKGRAY);}

EDIT: Also, I've never tried calling class in my .cpp before. So if you do the .cpp route, then you may also need to change it to: viewer.cpp

#include "viewer.h"
#include <raylib.h>

void Viewer::draw()
{
    ClearBackground(DARKGRAY);
}