Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 Hero Class
#1
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


Attached Files
.txt   hero_wrap.txt (Size: 70.34 KB / Downloads: 0)
"For God has not destined us for wrath, but for obtaining salvation through our Lord Jesus Christ," 1 Thessalonians 5:9

Maranatha!

The Internet might be either your friend or enemy. It just depends on whether or not she has a bad hair day.

[Image: SP1-Scripter.png]
[Image: SP1-Writer.png]
[Image: SP1-Poet.png]
[Image: SP1-PixelArtist.png]
[Image: SP1-Reporter.png]

My Original Stories (available in English and Spanish)

List of Compiled Binary Executables I have published...
HiddenChest & Roole

Give me a free copy of your completed game if you include at least 3 of my scripts! Laughing + Tongue sticking out

Just some scripts I've already published on the board...
KyoGemBoost XP VX & ACE, RandomEnkounters XP, KSkillShop XP, Kolloseum States XP, KEvents XP, KScenario XP & Gosu, KyoPrizeShop XP Mangostan, Kuests XP, KyoDiscounts XP VX, ACE & MV, KChest XP VX & ACE 2016, KTelePort XP, KSkillMax XP & VX & ACE, Gem Roulette XP VX & VX Ace, KRespawnPoint XP, VX & VX Ace, GiveAway XP VX & ACE, Klearance XP VX & ACE, KUnits XP VX, ACE & Gosu 2017, KLevel XP, KRumors XP & ACE, KMonsterPals XP VX & ACE, KStatsRefill XP VX & ACE, KLotto XP VX & ACE, KItemDesc XP & VX, KPocket XP & VX, OpenChest XP VX & ACE
Reply }




Users browsing this thread: