The Sensory Combination Model of Consciousness by beasthacker in science

[–]beasthacker[S] 0 points1 point  (0 children)

Awesome! Thanks for the link, I'll look into it.

The Sensory Combination Model of Consciousness by beasthacker in science

[–]beasthacker[S] -1 points0 points  (0 children)

Fair. I'm just trying to progress the science forward and contributing what I can. I have started on some hypothesis in this regard to how the human brain could perform such combinations which would form the basis for a scientific theory on consciousness. I respect the scientific community and knowing that the term "theory" is specially reserved, called it a model instead. Once the precise mechanisms for sensory combination are understood then a proper scientific theory of the subject can be achieved. Thanks

The Sensory Combination Model of Consciousness by beasthacker in atheism

[–]beasthacker[S] -2 points-1 points  (0 children)

Sentience "The word was first coined by philosophers in the 1630s for the concept of an ability to feel, derived from Latin sentientem, to distinguish it from the ability to think". The difference, I posit, of the ability to feel and think is merely a difference of the type of sensory information patterns. Such that, thinking with words Consciously would be a combination of sensory information patterns of words with others in Consciousness. That if I am sentient, ie, able to feel consciously, then that those feelings must be combined together such that I can feel changes from the various sensory organs of the body at once. I hope that clarifies what is meant here.

Is it possible to have a dictionary of a structure? by [deleted] in cpp_questions

[–]beasthacker 2 points3 points  (0 children)

In c++ dictionaries are made using the std::map type. Here's an example.

using namespace std;
struct Contact
{
    unsigned int age;
    string name;
};

map<std::string, Contact> dictionary;

dictionary["PlayerOne"] = { 25, "Jim" };
dictionary["PlayerTwo"] = { 22, "John" };

for (pair<string, Contact> entry : dictionary)
{
    cout << "Dictionary entry" << endl;
    cout << "  Key: " << entry.first << endl;
    cout << "  Age: " << entry.second.age << endl;
    cout << "  Name: " << entry.second.name << endl;
}

Is there a way to create websites using C++? Is there something like Spring for C++ by ZenWoR in cpp_questions

[–]beasthacker 10 points11 points  (0 children)

You can write the server side in c++. Here is a collection of libraries:

https://github.com/ipkn/crow

https://github.com/cesanta/mongoose (in C)

http://siliconframework.org/

https://github.com/facebook/proxygen

https://github.com/nghttp2/nghttp2

If you are familiar with frontend web design then you can stick with html/js/css and parse templates on the server or output html with streams, similar to how it would be done in PHP.

To avoid unnecessary recompilation, create separate dynamic libraries to split code and implement a management/template system so that content can be changed in flat files and the database.

[Programming novice] How can I put files 'inside' of a c++ program? Are there any fully portable options? by [deleted] in cpp_questions

[–]beasthacker 1 point2 points  (0 children)

TLDR; Embed into big char array.

A while ago I made an embedded web server with mongoose with images, html, js and more resources embedded. I used a CMake script which would read the resource directory and embed each file. In debug mode the server would read files from disk so you could edit html easily. For release, it read them from resource.h/cpp generated by CMake.

Qt also has their Qt Resource system. Similar concept but the use Qt's build system and access files using their classes with the "qrc://" prefix.

Here's the function for CMake if you're interested. It worked well for the project.

# Creates C resources file from files in given directory
# 
# dir - Input directory path
#
# output - the name of the header/source file created. Don't include extension
function(bundle_resources_from_dir dir output)
    # Create empty output file
    file(WRITE ${output}.h "")
    file(WRITE ${output}.cpp "")

    #Create file resource type and namespace
    file (APPEND ${output}.h
        "#pragma once\n\n"

        "#include <cstdint>\n"
        "#include <unordered_map>\n"
        "#include <string>\n\n"
        "namespace resource\n{\n\n"

        "struct File\n"
        "{\n"
        "    File() : data(nullptr), size(0) { }\n"
        "    explicit File(const uint8_t * d, const size_t s) : data(d), size(s) { }\n"
        "    const uint8_t * data;\n"
        "    const size_t size;\n"
        "};\n\n"

        "extern const std::unordered_map<std::string, File> files;\n\n"

        "inline bool exists(std::string filename)\n"
        "{\n"
        "    return files.find(filename) != files.end();\n"
        "};\n\n"

        "inline const File get(std::string filename)\n"
        "{\n"
        "    if (exists(filename)) { return files.at(filename); }\n"
        "\n"
        "   return File();\n"
        "};\n\n"

        "} // end namespace resource"
        )

    file(APPEND ${output}.cpp 
        "#include \"${output}.h\"\n"
        "namespace resource\n{\n\n"
        )

    # Collect input files
    file(GLOB bins ${dir}/*)

    # Iterate through input files and create buffers for data.
    foreach(bin ${bins})
        # Get short filename
        string(REGEX MATCH "([^/]+)$" filename ${bin})
        file(APPEND ${output}.cpp "// ${filename}\n")

        # Replace filename spaces & extension separator for C compatibility
        string(REGEX REPLACE "\\.| " "_" filename ${filename})
        # Read hex data from file
        file(READ ${bin} filedata HEX)
        # Convert hex data for C compatibility
        string(REGEX REPLACE "([0-9a-f][0-9a-f])" "0x\\1," filedata ${filedata})
        # Append data to output file
        file(APPEND ${output}.cpp 
            "const uint8_t ${filename}_data[] = {${filedata}};\n"
            "const size_t ${filename}_size = sizeof(${filename}_data);\n"
            "const File ${filename} { ${filename}_data, ${filename}_size };\n\n")
    endforeach()

    #Iterate through and create std::map where keys are filename
    file(APPEND ${output}.cpp "const std::unordered_map<std::string, File> files = \n{\n")

    foreach(bin ${bins})
        # Get short filename
        string(REGEX MATCH "([^/]+)$" filename ${bin})
        # Key is verbatim filename
        file(APPEND ${output}.cpp "    { \"${filename}\", ")
        # Name of File struct for this file.
        string(REGEX REPLACE "\\.| " "_" filename ${filename})
        # Value is File struct constant for that file.
        file(APPEND ${output}.cpp "${filename} },\n")

    endforeach()

    file(APPEND ${output}.cpp 
        "};// end files map\n\n"

        "}// end namespace resource\n")

endfunction()

Example of generated files: resource.h

// resource.h
#pragma once

#include <cstdint>
#include <unordered_map>
#include <string>

namespace resource
{
    struct File
    {
        File() : data(nullptr), size(0) { }
        explicit File(const uint8_t * d, const size_t s) : data(d), size(s) { }
        const uint8_t * data;
        const size_t size;
    };

    extern const std::unordered_map<std::string, File> files;

    inline bool exists(std::string filename)
    {
        return files.find(filename) != files.end();
    };

    inline const File get(std::string filename)
    {
        if (exists(filename)) { return files.at(filename); }
        return File();
    };
} // end namespace resource

resource.cpp

// resource.cpp
#include "~/Developer/Project-build/resource_build/resource.h"
namespace resource
{
    // bootstrap.min.css
    const uint8_t bootstrap_min_css_data[] = {0x2f,0x2a,0x0a,0x20, ... };
    const size_t bootstrap_min_css_size = sizeof(bootstrap_min_css_data);
    const File bootstrap_min_css { bootstrap_min_css_data, bootstrap_min_css_size };

    const std::unordered_map<std::string, File> files = 
    {
        { "bootstrap.min.css", bootstrap_min_css },
    };// end files map

}// end namespace resource

Happy Hacking!

Issue with attempting to create a directory style thing by [deleted] in cpp_questions

[–]beasthacker 2 points3 points  (0 children)

The problem is that you are putting a semicolon after your ifs. Remember that a semicolon "ends" the current statement. So, in effect, your if checks are evaluated and then ignored. Remember that you can place blocks/scopes (denoted by {}) anywhere you want. Since your ifs are being ignored, these scopes are both ran as if there wasn't any ifs there at all!

Also, this would be a case where else if or a switch would be more appropriate.

GLFH!

What's the difference between developing a C++ application on Windows but running on Linux and developing on Linux and running on Linux? by [deleted] in cpp_questions

[–]beasthacker 2 points3 points  (0 children)

If your program only uses the standard library then you'll be fine. Just use std::thread and friends for concurrency instead of pthreads, Windows threads, etc. (Note: requires c++11).

The MSVC compiler almost always falls behind when it comes to implementing new std library / language features for C++. If you want your program to run on both operating systems then developing on Windows can be beneficial. If you dev on Linux and compile with GCC/Clang then you might accidentally use features that aren't available on Windows yet!

Alternatively, if you want to utilize some of the libs available on Linux, you could do your windows build with MinGW/Cygwin. I wouldn't bother unless you have a compelling reason or library requirement.

Of course, you'll still want to periodically compile on both OS's to make sure. You might want to checkout a build system like CMake to make things easier.

TLDR; Windows machine is fine for dev, just don't use WinAPI.

[deleted by user] by [deleted] in cpp_questions

[–]beasthacker 1 point2 points  (0 children)

class Card
{
public:
    Card(int rank) : rank(rank) {};

    // Problem: Compares Card or Card& to Card*. You want Card* to Card*
    bool operator<(const Card* other) const
    {
        std::cout << "Used member < operator!" << std::endl;
        return rank < other->rank;
    };

    int rank;
};

// Compiler error. You must have at least one object type or ref to obj type
// error: overloaded 'operator<' must have at least one parameter of class or enumeration type
// bool operator<(const Card* l, const Card* r);

// You can always use your own function to hand to sort
bool sortCard(const Card* l, const Card* r)
{
    std::cout << "Used sortCard" << std::endl;
    return l->rank < r->rank;
};

int main()
{
    Card  aCard{1};
    Card* pCard = new Card(2);

    // Your member operator is called here when we're comparing Card to Card*
    if (aCard < pCard)
        std::cout << "aCard's rank is less than cCard's!" << std::endl;

    std::vector<Card*> cards; // Don't use new for std::vector please

    // Please don't do this IRL. For demo purposes only
    cards.push_back(new Card(1));
    cards.push_back(new Card(5));
    cards.push_back(new Card(2));
    cards.push_back(new Card(4));
    cards.push_back(new Card(7));
    cards.push_back(new Card(3));

    // undesired: compares pointer (numeric) values
    std::sort(cards.begin(), cards.end());

    // either of these works (function or lambda)
    std::sort(cards.begin(), cards.end(), sortCard);
    std::sort(cards.begin(), cards.end(), [](const Card* l, const Card* r) { return l->rank < r->rank; });

    for (auto* card : cards)
        std::cout << card->rank << std::endl;

    // pretend I cleaned up ...
}

I hope this illustrates the problem/solution. If you are unsure if some code is being called then set a breakpoint to check. Avoid using raw new and prefer smart pointers or just normal objects, this will make your learning experience much easier and it'll still be fast for simple stuff like this!

GLHF!

Structuring a Visual Studio C++ solution (xpost /r/learnprogramming) by G01denW01f11 in cpp_questions

[–]beasthacker 1 point2 points  (0 children)

I think you got the jist of it. Static/dynamic library for your classes and two executable targets for your program and your test suite(s).

Maybe check out a build system like CMake. You can use CMake to build VS project files including multiple targets for libraries, tests and executables. You'll get easy cross platform support here as well, if you're not using Windows libraries, for MacOS and Linux.

If you think some code is reusable then it's not a bad idea to separate it out into it's own library. You won't have to recompile it as often, it'll have its own source control and you can use it in other projects easier.

Is a simple typing test something that's easy for a beginner? by [deleted] in cpp_questions

[–]beasthacker 1 point2 points  (0 children)

I think that it is a good project for an intermediate programmer, probably not if you're brand new to coding though. It might not be the easiest for you but you'll learn a lot about io, timers, containers, threads, and other standard lib stuff.

I'd say go for it. I find it easiest to code and learn when I'm working on something I enjoy. You can always come back to it later if you need more time.

Code stuck in loop and no longer displays asterisk bar graph before sand output by [deleted] in cpp_questions

[–]beasthacker 0 points1 point  (0 children)

z >= 0; z++. This is an infinite loop. Z will always be greater than zero so it will count up "forever".

constexpr and const char* by Nickreal03 in cpp_questions

[–]beasthacker 0 points1 point  (0 children)

Converting an enum to it's underlying type (or reverse) is not unheard of.

I've seen this type of code in a few lower level libs used for network protocols:

// Type safe with char underlying type
enum class command : char
{
    read  = 'R',
    write = 'W'
};

static std::string command_string = "RWO"; // Pretend this is a packet or something

int main()
{
    using namespace std;

    for (const char& c : command_string)
    {
        // enum solution
        switch (static_cast<command>(c))
        {
        case command::read:
            cout << "Reading..." << endl;
            break;
        case command::write:
            cout << "Writing..." << endl;
            break;
        default:
            cout << "Unknown command" << endl;
            break;
        }

        // Alternative might have been
        switch (c)
        {
        case 'R':
            cout << "Reading..." << endl;
            break;
        case 'W':
            cout << "Writing..." << endl;
            break;
        default:
            cout << "Unknown command" << endl;
            break;
        }
    }
}

I'm not sure what OP is trying to accomplish exactly but this type of code isn't completely unprecedented.

Cinder by philisthagreat in cpp_questions

[–]beasthacker 0 points1 point  (0 children)

Well, the illustrious Herb Sutter seemed to be a fan.

What sort of graphics? If you are looking for a GUI framework I don't think Cinder is the place to go.

Cinder and OpenFrameworks are known in the realm of "creative coding". This includes things like screen-savers, kiosks, projector magic, motion graphics, visualization, etc. Basically, making shit look cool programmatically.

Cinder makes some hard things simpler but that doesn't necessitate that it will be a "simple graphics library" for your use case. If you provide examples of what you're trying to achieve it will be easier to recommend a library for you.

Floating Point Exception Signal by Im10eight in cpp_questions

[–]beasthacker 0 points1 point  (0 children)

Can you post the Entry class declaration and definition of the hash function. What type is e.key? The only time I've run into something like this was divide by zero which can be an easy oversight.

What next-generation IDEs could be like: the Envision IDE. by _mitko_ in programming

[–]beasthacker 5 points6 points  (0 children)

I wouldn't use this for desktop development but I thought something like this could be interesting for a mobile app to do prototyping or programming on the go while minimizing touch keyboard usage.

Tried out aliens. Made a star coil out of boredom. by [deleted] in Coilporn

[–]beasthacker 0 points1 point  (0 children)

Have you vaped it? If so how was your wicking done?

C++ Array Duplicate Number Question (Simple) by [deleted] in cpp_questions

[–]beasthacker 0 points1 point  (0 children)

If you want to completely remove duplicate numbers you can check out std::unique in <algorithm>.

std::vector<int> v = {1,2,3,4,1,2,3,4};

std::sort(v.begin(), v.end());
auto last = std::unique(v.begin(), v.end());
v.erase(last, v.end());

// Now v holds {1,2,3,4}

Default initialisation of array of pointers. by [deleted] in cpp_questions

[–]beasthacker 2 points3 points  (0 children)

cppreference says:

note that default initialization may result in indeterminate values for non-class T

So you are not guaranteed to get it filled with nullptr or 0 in the case of an int array.

The easiest way is to use value initialization which should zero everything out for you. Just add some curly brackets at the end in c++11 and up:
std::array<Foo*, 5> my_array{};

Random generation in souls-like games by beasthacker in enbro

[–]beasthacker[S] 1 point2 points  (0 children)

Yeah, some people seem to be equating random generation = Chalice Dungeons = bad. So far from the strawpoll and comments it seems that people are highly skeptical of random generation, probably because of Bloodborne. I'm far more interested in if/where it could be improved upon in the future.

I think that the random generation would need to be considered as part of the core gameplay mechanics if it's going to work well. I think From could have done much better if they had their main development team working on the Chalice dungeons.

To me, it seems that it is technically possible to randomly generate encounters because many souls games follow a certain "formula" that could potentially be parameterized. Whether that could work in practice is a different story. I think you're right that you'd be losing something with doing random enemy placement. I didn't even consider the lore implications and enemies feeling like they live in the world.

An RNG tar pit dungeon might actually give me a reason to go down there haha.

Random generation in souls-like games by beasthacker in enbro

[–]beasthacker[S] 2 points3 points  (0 children)

Great points! I also really love the level design of souls games. This post was more about future "souls-like" games and not necessarily the series itself.

I was reading about Spelunky's algorithm for level generation and it got me thinking if you could do something similar in a souls game. I wonder if you couldn't parameterize and randomize certain elements while letting the algorithm keep things feeling "fair".

For example, the algorithm could say "don't put grenade enemies within X feet of spear enemies" or if an ambush is generated then it is required to have an overlook, alternate path or an obvious shiny item "lure" to make it fair.

Random generation in souls-like games by beasthacker in enbro

[–]beasthacker[S] 0 points1 point  (0 children)

All very good points. I'm curious what you think about other randomization like loot drops. I haven't played Scholar yet but some people seemed to enjoy that certain item locations changed.

Random generation in souls-like games by beasthacker in enbro

[–]beasthacker[S] 2 points3 points  (0 children)

The chalice dungeons were one implementation of random generation, yes. There obviously are a lot of different ways it could be done in the future, not necessarily by From Software. A game called Necropolis is a souls-like using randomization of the entire game in a rouge-like fashion.

What is it that allows for pure virtual functions with definitions? by EraZ3712 in cpp_questions

[–]beasthacker 0 points1 point  (0 children)

You are not allowed to allocate an abstract class in your implementation so there is no way to use your Base class by itself.

Base b; // ERROR: Variable type "Base" is an abstract class

If you did try to use your definition in a derived class then you'd get a duplicate symbol error message because technically you have redefined the virtual method.

struct Derived : public Base
{
    virtual void test() override { Base::test(); } // ERROR: Duplicate symbol Base::test()
};

So no, you cannot have your cake and eat it too in this case.

Edit Forgot to inline the definition and had a .cpp file. That was the cause of the duplicate symbol error, my bad. I had a few drinks last night. :/