Save-Point
Hero Class - Printable Version

+- Save-Point (https://www.save-point.org)
+-- Forum: Games Development (https://www.save-point.org/forum-4.html)
+--- Forum: Development Discussion (https://www.save-point.org/forum-17.html)
+--- Thread: Hero Class (/thread-7357.html)



Hero Class - kyonides - 11-02-2018

What's the main purpose of this thread?

I'm not quite sure about it, guys. Laughing  I guess it's showing off these pieces of code in case any of you never have seen any code not written in Ruby or Javascript ever. This post will sport lots? of C and C++ code and no useful comment besides those few comments found in the SWIG wrapper code.

Since we're dealing with a simple template class like an RPG-ish Hero class, I guess it's not needed to understand most of it. It mainly features very few components that can be compiled with stuff like g++ to make it run on a PC. The compiled file won't require more than 50 KB of space on your harddisks.

A basic Hero Class in C++

Header File

Code:
#ifndef HERO_H
#define HERO_H

#include <cstdio>
#include <cstdlib>
#include <iostream>

class Hero
{
 public:
   int age;
   int class_id;
   std::string name;
   std::string nickname;
   std::string description;
   Hero();
   Hero(const Hero &other);
   ~Hero();
};

#endif


C++ (aka CPP or CXX) File

Code:
#include "hero.h"

Hero::Hero() {
 name = "Name";
 nickname = "Nickname";
 description = "Describe me";
 age = 0;
 class_id = 0;
}

Hero::Hero(const Hero &other) {
 name = other.name;
 nickname = other.nickname;
 description = other.description;
 age = other.age;
 class_id = other.class_id;
}

Hero::~Hero() {}

A basic SWIG intermediate file
Code:
%module hero
%{
#include "hero.h"
%}
%include hero.h


There's also a hero_wrap.txt file, but its file extension should be cxx or cpp instead, guys.

If you ever open it, you'll notice it contains what the intermediate SWIG file created based on my two previous files. Of course I gotta admit I "heavily" edited it to suit my needs... Like creating a custom Hero class instead of a module, SWIG's default behavior is to make modules for Ruby scripts to require it and any classes defined there are defined under that specific module. Since I'm kind of comfortable with CRuby (Ruby code written in ugly C code), I managed to change how it works and now I can easily create a new Hero class by calling...

@hero = Hero.new
puts @hero.name #=> Name (default value)
puts @hero.age #=> 0 (default value)

OK, I forgot to mention something important here... Happy with a sweat 

You gotta compile it yourself on your PC to make it work unless you've got a Linux distribution that can run my binary executable with ease via Ruby's irb console or terminal or MSDOS or command line like interface.

(Usually if you've been working on a Linux distro for a few years at least, you gotta be capable of making more complicated stuff like the one above Confused  )

Making the same Hero class in plain old Ruby 
Code:
class Hero
  def initialize
    @name = "Name"
    @nickname = "Nickname"
    @description = "Describe me"
    @age = 0
    @class_id = 0
  end
  attr_accessor :name, :nickname, :description, :age, :class_id
end

Yeah, it obviously looks quite "simple" in this format... Laughing  Plus it will automatically handle saving and loading its data, saving you a lot of headaches in the process. But add 1 or more Table classes and it might not be that great after all. Confused 

Now I gotta find out if I can really share the C++ part of the code with KDE / Qt5 or wxWidgets or any C/C++ GUI framework. Sarcasm

I hope someday people will then appreciate Ruby's delightful syntax after reading pure C/C++ code... Winking
By the way, there should be a General (or C/C++) tag here for topics like this one. Laughing + Tongue sticking out