Jump to content

  • Log In with Google      Sign In   
  • Create Account


Member Since 05 Jul 2002
Offline Last Active Yesterday, 11:54 PM

#5246789 How can this code be refactored to be more proper OOP?

Posted by Alpheus on 15 August 2015 - 05:32 PM

How can the code below be refactored to remove the set and get methods? And more importantly should they be and why?

// Code taken from somewhere
#include "app.h"
#include "enemy.h"
Enemy::Enemy(App* app, const char* file,int screenWidth, int screenHeight, SDL_Renderer* renderer) {
        x = 0;
        y = 0;
        w = 0;
        h = 0;
        fireTicks = 0;
        fireRate = 400;
        velocity = 250;
        alive = false;
        enemyBulletTex = NULL;
        this->app = app;
        if((enemyTex = gfxManager::loadTexture(renderer,file)) == 0) {
                throw std::invalid_argument("err, could not load the enemy texture!");
    if((enemyBulletTex = gfxManager::loadTexture(renderer, "../gfx/enemyBullet_1.png")) == 0) {
        throw std::invalid_argument("err, could not load the enemyBullet texture!");
    SDL_QueryTexture(enemyTex, NULL, NULL, &w, &h);
    x = (screenWidth - w) /2;
    y = 150;
Enemy::~Enemy() {
void Enemy::think(float elapsedSeconds) {
    if(isAlive()) {
        // boundaries detection
        if(x < 0) {
            x -= x;
        if(x + w > app->getWidth()) {
            x -= (x + w - app->getWidth());
void Enemy::render(SDL_Renderer *renderer) {
void Enemy::shoot() {
    int currentTime = SDL_GetTicks();
    if(fireTicks + fireRate < currentTime) {
        for(int i = 0; i < app->getBulletPoolSize(); i++) {
            if(app->getBulletPool()[i]->getActive() == false) {
                app->getBulletPool()[i]->setX((x + w / 2)-app->getBulletPool()[i]->getW()/2);
                app->getBulletPool()[i]->setY(y + h / 2);
        fireTicks = currentTime;
void Enemy::setAlive(bool status) {
    alive = status;
bool Enemy::isAlive() {
    return alive;
int Enemy::getX() {
    return x;
int Enemy::getY() {
    return y;
int Enemy::getW() {
    return w;
int Enemy::getH() {
    return h;

#5246307 Is Javascript easy if you know basic C++

Posted by Alpheus on 13 August 2015 - 03:31 PM

Typescript is my favorite,


That would be the most reasonable transition. But if he's doing Android work, I really don't know what Javascript has to do with it. If he wants to make games for the web that can be accessed by his Android, then cool. But native apps? That tree he's next to has "Don't bark up here" sign on it.

#5246226 Is Javascript easy if you know basic C++

Posted by Alpheus on 13 August 2015 - 09:10 AM

Before we go down the rabbit hole, what exactly are you trying to do? Find more challenging projects, be a web developer, or be a mobile app developer?


Answering this question concisely will help us help you :)

#5246215 Is Javascript easy if you know basic C++

Posted by Alpheus on 13 August 2015 - 08:29 AM

Once you know a language (and 'know' is subjective) you can learn another similar language without too much trouble. However, javascript handles classes, inheritance, and few other things very differently than C++. Javascript is actually a "Prototypical" language. Non-trivial language features revolve around functions and prototypes. Also, references to parent or base classes (functions) are handled differently in Javascript than in C++.


In short, you can learn Javascript after learning C++. But learn Javascript and don't try to compare it to C++. If you do, you'll be causing yourself more trouble than needed.

#5244844 About to start my graphics course

Posted by Alpheus on 06 August 2015 - 09:49 AM

Graphics can be something as simple as putting animated characters and objects on a screen. Or it can be an approximation of what we see in real life. Or anything in-between.


Expect heavy use of and solid in knowledge in: geometry, linear algebra, and calculus. Expect to translate that math into code.


It's math and code.

#5242979 To use mana or not? (that is the question)

Posted by Alpheus on 27 July 2015 - 08:58 AM

When you encounter monsters, combat is turned-based and each character can perform one attack or special skill (based on class) per round. However, there is several ways to handle this:
1. All skills (including basic attacks) are "equally good" and you can choose anyone each combat round (used in darkest dungeon)
2. Like 1 but each skill has a cooldown of X turns. More powerful ones have longer cooldown. Cooldowns only refill during combat, not while exploring (so CDs will remain into next fight).
3. Skills cost mana/energy to use, so more powerful skills use more mana, forcing you to plan. It regens in town only (maybe with rare potions as well.
4. Use both CD and energy (like most MMORPGs, like wow)
5. No mana is used. Skills are limited use. So you can only use ice bolt 2 times and healing wave 1 time. Resets in town.
What are your thoughts on this? Each character already has HP and morale bars, so another bar (mana/energy) might be too much.

1. Aside from variety, I don't see the upside to this.
2. Cooldown should refill during exploration. IE. they should be reset at the start of every battle.
3. Standard and fair.
4. IMO, that's a bit punitive to the player. Is there an in-battle reward in using spells? Is there a way to overcome this as a team? EX: can a player speed up cooldowns of other players and/or replenish mana of other players?
5. I'd save that for the Legendary difficulty setting.

#5242665 Breaking out of a nested loop

Posted by Alpheus on 25 July 2015 - 01:52 PM

If you've got two for loops you're looking at O(n*n) execution times.


I'd be tempted to refactor the code and eliminate the for loops if possible but that's just me...


In all seriousness, I'd be interested to see that.

#5242176 Where to begin

Posted by Alpheus on 23 July 2015 - 08:38 AM

This might help:

#5241597 Know almost nothing about Game Development; Here to learn

Posted by Alpheus on 20 July 2015 - 02:53 PM

Go to Tom Sloper's forum (here on GameDev) and read up on the information there.

#5241219 Where to begin

Posted by Alpheus on 18 July 2015 - 10:17 AM

Take a quick read of this article: http://www.gamedev.net/page/resources/_/technical/game-programming/your-first-step-to-game-development-starts-here-r2976


Then google any of the games listed in the article to learn how to program them.

#5241189 Is there software that creates Game Design Documents?

Posted by Alpheus on 18 July 2015 - 04:41 AM

Well the OP of this thread seems to have made some GDD creation software. I thought it was interesting. So, again, I wondered if there was anything else like it. But it seems most people just use a collection of tools.

#5240862 How do you write this down?

Posted by Alpheus on 16 July 2015 - 12:11 PM


bool meow = (v > 0);

Comp comp = (meow? std::less : std::greater);
Oper next = (meow? std::next : std::prev);
Oper prev = (meow? std::prev : std::next);

Func predicate = (meow? doA : doB);
Func operation = (meow? doB : doA);

    y+=x; // keeps the same
    z+=y; // keeps the same



But honestly, depending on what your code actually does, with proper variable names and descriptive comments, I probably would prefer your original code just branching into two separate loops (especially if this section of code is a performance bottleneck). Sometimes code that appears superficially similar can be overly generalized to the harm of your codebase.


I must say that is the most elegant C++ I have ever seen.


I am honored to be witness to such an event.

#5240604 Improving my architecture design and career

Posted by Alpheus on 15 July 2015 - 03:46 PM

Pragmatic Programmer is a good start. There are two more books on Design Patterns that I'm reading currently. I'll post the names later.



Adaptive Code via C#: Agile coding with design patterns and SOLID principles


C# Design Pattern Essentials

#5240365 New Game Programmer!

Posted by Alpheus on 14 July 2015 - 06:02 PM

The first question is this: Have you ever program a simple, plain Hearts game?


If the answer to this question is no, then I would suggest doing that before committing to any "improvements" on the game or the version of the game itself.


If this is the first game you've ever programmed and your goal is to write code and create games with that code (ie. game developer/programmer), then you should probably write the game from scratch.

#5240121 Explain: Finite State Machines

Posted by Alpheus on 13 July 2015 - 02:19 PM

Wait. You're the Tutorial Doctor. Why didn't you just read a tutorial?