u/Recon1379

Worth it going into CS teaching?

Hey everyone! I wanted to ask if its worth going into teaching CS as a career path. I spent a long time trying to decide what i wanted to do with my degree and my overall dream is be an game engine programmer or have my own studio someday, but until i acquire the skills and money for that job Ive been considering doing teaching a temporary career as i seriously considered it before graduation as i love learning as a whole and like teaching others about CS. I wanted to get any opinions on the CS teaching space and wanted to see how other people are enjoying it. This is probably a dumb question but I'm feeling a little anxious about if i should commit to it or not.

reddit.com
u/Recon1379 — 12 days ago

global classes and when its ok to use them?

Making a game engine in cpp, rewriting and refactoring a lot of stuff using advice I got from here. still learning cpp practices and wanted to ask a few quesions about global classes or make classes globally accessable. Im rewriting my logger as before i had it as just C style free function, trying to adapt to more modern practices in cpp.

1.singletons

I know singletons are generally not favored, would a logger for an engine be a place where its okay?

  1. extern or inline

ive used extern for making global classes before and it worked but was kinda tedious and ive seen people use inline for the same thing, how do they compare and would it be reccomended? Ex

header file:

#pragma once
Class My_Object{}

//extern My_Object object
//inline My_object object

source file:

#inlude My_object.h

My_Object object

  1. Any other suggestions?

I know this isn't all the ways you can globalize or pass a class around, and i know the norm usually is having a class member variable in what other classes that need it but I don't like having to create a variable for logger every time I need it in a piece of my engine.

yes i know the logger isn't super important in the grandscale of the engine and it won't really do much when im ready to make what ever game im gonna make with it. I just like having it cause i feel like it helps me keep track of where things are.

I have a psuedo code implementation of what I want that class structure to be but wanted to get more suggestions. I am using CPP 17 for this engine.

reddit.com
u/Recon1379 — 1 month ago

have a free function logger header and cpp file without needing to use platform class variable or instance?

EDIT: This is a edit of a post from yesterday that i made, my question was very vague so i hope this give better context. I hope the people who responded to my last post see this!

I've been following the KOHI engine series, he does things in C so I've been translating to C++ so i can understand things better, hence why I'm restructuring.

The code below shows the platform class with that weird global forward declaration thing, i would paste the cpp file but its too large so i just did the header file.

That small if(is_error) is the only platform code in the logger, the logger is just a header and cpp file with free functions no class, i want to keep it that way so i can log things from anywhere with it depending on anything. some of the platform functionality is also called in some other stuff but if i can figure out the logging problem than the others should be easy to fix.

I've tried some suggestions in the comments on my last post but without rewriting the entire application, platform and other lower level system this proved difficult. Hopping for more advice/suggestions!

#include "defines.h"
#include "core/event.h"


typedef struct platform_state{
    void* internal_state;
}platform_state;


class Platform {
    
    public:



        FAPI b8 platform_startup(
        platform_state* plat_state,
        const char* application_name,
        i32 x,
        i32 y,
        i32 width,
        i32 height);



        FAPI void platform_shutdown(platform_state* plat_state);


        FAPI b8 platform_pump_message(platform_state* plat_state);


    void* platform_allocator(u64 size, b8 alligned);
    void platform_free(void* block, b8 aligned);
    void* platform_zero_memory(void* block, u64 size);
    void* platform_copy_memory(void* dest, const void* source, u64 size);
    void* platform_set_memory(void* dest, i32 value, u64 size);


    


    f64 platform_get_absolute_time();


    //sleep should thread for the provided ms. this blaocks the main thread
    //should only be used for giving time back to the OS for unsued update powere
    //therefor it is not exoported
    void platform_sleep(u64 ms);
};


void platform_console_write(const char* message, u8 colour);
void platform_console_write_error(const char* message, u8 colour);


extern Platform* platform;

void log_output(log_level level, const char* message, ...){
    const char* level_string[6] = {"[FATAL]: ", "[ERROR]:  ","[WARN]:  ","[INFO]:  ","[DEBUG]:  ","[TRACE]:  ",};
    b8 is_error = level < LOG_LEVEL_WARN;


    //NOTE: declare this array and 
    //zero out the memory, faster then 
    //dynamic mem alloc using malloc()
    //this is done on the stack
    const i32 msg_length = 32000;
    char out_message[msg_length]; 
    memset(out_message, 0, sizeof(out_message));


    //takes arugment list, starts using it to perform opertaions on it.
    __builtin_va_list arg_ptr;
    //starts after the message argument
    va_start(arg_ptr, message);
    vsnprintf(out_message, msg_length, message, arg_ptr);
    va_end(arg_ptr);



    //output to outmessage, 
    //auto append new line character and log level, then print message
    char out_message2[msg_length];
    sprintf(out_message2, "%s%s\n", level_string[level], out_message);


    //platform-sepecific output.
    if(is_error){
        platform_console_write_error(out_message2, level);
    
    }else{
        platform_console_write(out_message2, level);
    }
}
reddit.com
u/Recon1379 — 1 month ago

Free function in another file needs access to class member function

Im making a game engine from scratch and I'm reorganizing some things to get better at cpp, I have a logger thats just some free functions but its uses some platform code for certain errors. My platform layer is a class and currently i have just a global object declared in the platform class like this.

#include "defines.h"
#include "core/event.h"


typedef struct platform_state{
    void* internal_state;
}platform_state;


class Platform {
    
    public:



        FAPI b8 platform_startup(
        platform_state* plat_state,
        const char* application_name,
        i32 x,
        i32 y,
        i32 width,
        i32 height);



        FAPI void platform_shutdown(platform_state* plat_state);


        FAPI b8 platform_pump_message(platform_state* plat_state);


    void* platform_allocator(u64 size, b8 alligned);
    void platform_free(void* block, b8 aligned);
    void* platform_zero_memory(void* block, u64 size);
    void* platform_copy_memory(void* dest, const void* source, u64 size);
    void* platform_set_memory(void* dest, i32 value, u64 size);


    


    f64 platform_get_absolute_time();


    //sleep should thread for the provided ms. this blaocks the main thread
    //should only be used for giving time back to the OS for unsued update powere
    //therefor it is not exoported
    void platform_sleep(u64 ms);
};


void platform_console_write(const char* message, u8 colour);
void platform_console_write_error(const char* message, u8 colour);


extern Platform* platform;

void log_output(log_level level, const char* message, ...){
    const char* level_string[6] = {"[FATAL]: ", "[ERROR]:  ","[WARN]:  ","[INFO]:  ","[DEBUG]:  ","[TRACE]:  ",};
    b8 is_error = level < LOG_LEVEL_WARN;


    //NOTE: declare this array and 
    //zero out the memory, faster then 
    //dynamic mem alloc using malloc()
    //this is done on the stack
    const i32 msg_length = 32000;
    char out_message[msg_length]; 
    memset(out_message, 0, sizeof(out_message));


    //takes arugment list, starts using it to perform opertaions on it.
    __builtin_va_list arg_ptr;
    //starts after the message argument
    va_start(arg_ptr, message);
    vsnprintf(out_message, msg_length, message, arg_ptr);
    va_end(arg_ptr);



    //output to outmessage, 
    //auto append new line character and log level, then print message
    char out_message2[msg_length];
    sprintf(out_message2, "%s%s\n", level_string[level], out_message);


    //platform-sepecific output.
    if(is_error){
        platform_console_write_error(out_message2, level);
    
    }else{
        platform_console_write(out_message2, level);
    }
}

EDIT: This is a better look at what I'm doing, I've been following the KOHI engine series, he does things in C so I've been translating to C++ so i can understand things better, hence why I'm restructuring.

The code above shows the platform class with that weird global forward declaration thing, i would paste the cpp file but its too large so i just did the header file.

That small if(is_error) is the only platform code in the logger, the logger is just a header and cpp file with free functions no class, i want to keep it that way so i can log things from anywhere with it depending on anything. some of the platform functionality is also called in some other stuff but if i can figure out the logging problem than the others should be easy to fix.

I've tried some suggestions in the comments but without rewriting the entire application, platform and other lower level system this proved difficult. lots of yall said my question before was vague so i hope this adds more context!

reddit.com
u/Recon1379 — 1 month ago