Home > Blockchain >  Game Engine: Class paramater
Game Engine: Class paramater

Time:11-14

I am doing university work for a game engine and we needed to do an agnostic code. They told us to do the rest on our own and I am on the right track however, when I try and pass the parameters for the function it says it doesn't pass for the arguments? This I believe is due to the class paramater that I have on there. Am I being an idiot or ?

Error: (without Buffer Layout) function arguments don't match. Or with the code you can see it says "Expected an expression"

Please help!

Vertex Buffer

/* \file OpenGLVertexBuffer.cpp */
#include "engine_pch.h"
#include <glad/glad.h>
#include "include/independent/platform/OpenGL/OpenGLVertexBuffer.h"


namespace Engine
{
    void BufferLayout::addElement(BufferElement element)
    {
        m_elements.push_back(element);
        calcStrideAndOffset();
    }

    void BufferLayout::calcStrideAndOffset()
    {
       uint32_t l_offset = 0;

        for (auto& element : m_elements)
        {
            element.m_offset = l_offset;
            l_offset  = element.m_size;
        }

        m_stride = l_offset;
    }


    OpenGLVertexBuffer::OpenGLVertexBuffer(void * vertices, uint32_t size, BufferLayout layout) : m_layout(layout)
    {
        glCreateBuffers(1, &m_OpenGL_ID);
        glBindBuffer(GL_ARRAY_BUFFER, m_OpenGL_ID);
        glBufferData(GL_ARRAY_BUFFER, size, vertices, GL_DYNAMIC_DRAW);
    }

    OpenGLVertexBuffer::~OpenGLVertexBuffer()
    {
        glDeleteBuffers(1, &m_OpenGL_ID);

    }

    void OpenGLVertexBuffer::edit(void * vertices, uint32_t size, uint32_t offset)
    {
        glBindBuffer(GL_ARRAY_BUFFER, m_OpenGL_ID);
        glBufferSubData(GL_ARRAY_BUFFER, offset, size, vertices);
    }

}

Rendering API

/* \file renderAPI.cpp */

#include "engine_pch.h"

#include "rendering/RenderAPI.h"
#include "rendering/indexBuffer.h"
#include "rendering/vertexBuffer.h"

#include "platform/OpenGL/OpenGLIndexBuffer.h"
#include "platform/OpenGL/OpenGLVertexBuffer.h"

#include "systems/log.h"

namespace Engine
{
    RenderAPI::API Engine::RenderAPI::s_API = RenderAPI::API::OpenGL;

    IndexBuffer * IndexBuffer::create(uint32_t * indices, uint32_t count)
    {
        switch (RenderAPI::getAPI())
        {
        case RenderAPI::API::None:
            Log::error("No supported Rendering API");

        case RenderAPI::API::OpenGL:
            return new OpenGLIndexBuffer(indices, count);

        case RenderAPI::API::Direct3D:
            Log::error("Direct3D not currently Supported!");

        case RenderAPI::API::Vulkan:
            Log::error("Vulkan not currently Supported!");


        }
    
    }

    VertexBuffer * VertexBuffer::edit(void* vertices, uint32_t size, uint32_t offset)
    {
        switch (RenderAPI::getAPI())
        {
        case RenderAPI::API::None:
            Log::error("No Supported Rendering API");

        case RenderAPI::API::OpenGL:
            //return new OpenGLVertexBuffer(vertices, sizeof(size), offset);
            new OpenGLVertexBuffer(vertices, size, <BufferLayout> offset);
        }
    }

}

CodePudding user response:

The call to "new OpenGLVertexBuffer" expects three arguments

  • a void*
  • an uint32_t
  • a BufferLayout

You pass

  • vertices, a void* (okay)
  • sizeof(size) - wrong. It calculates the size of the variable(!) size, which is 4 (uint32 is always 32 bits/4 bytes). So you pass the constant 4 as the second parameter. Not what you want.
  • offset - wrong again. The third parameter is expected to be of type BufferLayout. You are trying to pass a uint32_t, where you should be passing a BufferLayout.

In general you need to read up on types (why is uint32_t not a BufferLayout), functions (parameters), constructors.

Also you are missing break statements in your switch. Study the switch statement to see what I mean.

And you are using pointers (return new something). If they teach you that in university then the course you are taking is very outdated. As a beginner you should NEVER have to use pointers, NEVER work with new. You could use the C standard ways of std::shared_ptr<VertexBuffer> instead, using std::make_shared... rather than new. The problem with pointers is that they are just too easily mishandled. You will forget to call delete in the proper place(s), which will lead to memory leaks. Or you will call delete in the wrong places, which will lead to 'use after free' or 'calling delete for a deleted pointer'. shared_ptr and similar classes take off the burden and will do the work for you.

The need for pointers has been greatly diminished in modern C .

Finally: When you post a question, please include a: the exact compiler error b: the exact location, where this error occurs.

You could for example include a // ERROR MESSAGE HERE comment in your example code. Both points will make it much more likely that someone answers your questions.

  • Related