Bay 12 Games Forum

Please login or register.

Login with username, password and session length
Advanced search  
Pages: 1 ... 9 10 [11] 12 13 ... 91

Author Topic: Programming Help Thread (For Dummies)  (Read 100518 times)

olemars

  • Bay Watcher
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #150 on: May 29, 2011, 03:04:58 pm »

The style is quite good, especially for beginner level.

The attack class is a bit superfluous in its current form, but it's a good idea as events get more complicated. What I don't like is how you new the attack, pass the pointer off to another ship object and delete it there. That's a bad habit that will easily cause meory leaks and crashes in the lon run. In this case you would probably be better off using pass-by-value instead of pointers.
Logged

Angle

  • Bay Watcher
  • 39 Indigo Spear Questions the Poor
    • View Profile
    • Agora Forum Demo!
Re: Programming Help Thread (For Dummies)
« Reply #151 on: May 29, 2011, 03:07:08 pm »

Siquo: Ah! Initializer lists! I remember those! But I thought they could only set things to values, and not variables? edit = seems they handle variables just fine, but they don't like me using the scope operator like I was.

Olemars: So I should declare a varable,  set the variable to the new attack, pass that variable by value, then delete that variable?

Spoiler: Like this? (click to show/hide)
« Last Edit: May 29, 2011, 03:41:47 pm by Angle »
Logged

Agora: open-source platform to facilitate complicated discussions between large numbers of people. Now with test site!

The Temple of the Elements: Quirky Dungeon Crawler

olemars

  • Bay Watcher
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #152 on: May 29, 2011, 03:38:18 pm »

More like

Code: [Select]
bool Ship::attack(Ship& target)
{
    for (int i = 0; i < weapons; i++)
    {
        Attack attack(damage, targeting);
        if (target.defend(attack))
        {
            return 1;
        }
    }
    return 0;
}

which is why I mentioned the Attack class as a bit superfluous in this case, the simplest would be to just send the two parameters directly.
Logged

Max White

  • Bay Watcher
  • Still not hollowed!
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #153 on: May 29, 2011, 03:47:25 pm »

Also, and I don't think this would be required for your current project, but if you really want to, I would have your ship class throw a custom exception rather than print to the console. Then have your main catch it, as it seems be be the controller/user interface for this project. That way you don't have your model coupled to your user interface, and if you want to change something later on it is much easier.

Angle

  • Bay Watcher
  • 39 Indigo Spear Questions the Poor
    • View Profile
    • Agora Forum Demo!
Re: Programming Help Thread (For Dummies)
« Reply #154 on: May 29, 2011, 03:59:04 pm »

Oh... I don't need to use dynamic memory at all. whoops.

The reason for the attack class is that I'm going to be making things much more complicated. I'm planning to change the ships from being stat based to being made of a whole bunch of components, and am also going to move from one kind of damage and one kind of attack to a whole lot of kinds of damage and a whole lot of kinds of attacks, e.g. lasers, mass drivers, missiles, all the different varieties of lasers, mass drivers, and missiles, etc. I'm also thinking about moving away from simple HP to having a wounds system, where different attacks will result in different kinds of damages to your ship, like a laser beam might go and pierce through your armor and melt a critical part of one of the ammo feeds to your missiles, or a mass driver might launch a whole lot of small projectiles that don't really pierce your ship, but do shred up your armor a lot. This is obviously a little while off, but I figure I might as well not make things any more difficult for me when I get there.

Max White: you mean the output for the attack and defend functions, right?
Logged

Agora: open-source platform to facilitate complicated discussions between large numbers of people. Now with test site!

The Temple of the Elements: Quirky Dungeon Crawler

Max White

  • Bay Watcher
  • Still not hollowed!
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #155 on: May 29, 2011, 04:02:30 pm »

Sort of. the attack (Ship target) function isn't an offender because it isn't writing anything to the console. Your defend class, however, is.

Angle

  • Bay Watcher
  • 39 Indigo Spear Questions the Poor
    • View Profile
    • Agora Forum Demo!
Re: Programming Help Thread (For Dummies)
« Reply #156 on: May 29, 2011, 04:14:38 pm »

The problem with using exceptions is that they break the normal flow of the program. I don't want the flow to jump straight from the defend function back up to main, I want it to finish the attack function first.
Logged

Agora: open-source platform to facilitate complicated discussions between large numbers of people. Now with test site!

The Temple of the Elements: Quirky Dungeon Crawler

Max White

  • Bay Watcher
  • Still not hollowed!
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #157 on: May 29, 2011, 04:21:47 pm »

Not always. If you have one layer catch the exception, clean up your resources, then throw it to the next level up, then you don't need to worry about some of the ramifications of throwing stuff around like leeks. It is possible to use the throw keyword very effectively to minimize dependencies.

Angle

  • Bay Watcher
  • 39 Indigo Spear Questions the Poor
    • View Profile
    • Agora Forum Demo!
Re: Programming Help Thread (For Dummies)
« Reply #158 on: May 29, 2011, 04:26:58 pm »

But I still have flow trouble. Attack is supposed to call defend for each weapon, not just once.
Logged

Agora: open-source platform to facilitate complicated discussions between large numbers of people. Now with test site!

The Temple of the Elements: Quirky Dungeon Crawler

Max White

  • Bay Watcher
  • Still not hollowed!
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #159 on: June 03, 2011, 02:45:02 am »

Brothers in code, come to my aid!
I have plans for a game drawn up, and I'm currently looking at the tec side of things, starting with supporting services. I want this game to be cross platform, so hello Java! But I want it to be a graphical roguelike, similar to Stone soup - tiles, Elona, and dwarf fortress with a graphics pack. Need supporting library for graphics, fire at will. Pros and cons are encouraged, and if you know several throw them all out, as I like to keep alternatives in my docs.

olemars

  • Bay Watcher
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #160 on: June 03, 2011, 02:57:19 am »

I know nothing about it, but LWJGL seems popular.
Logged

Max White

  • Bay Watcher
  • Still not hollowed!
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #161 on: June 03, 2011, 03:03:08 am »

Well if the name is anything to go off, it sounds ideal! Hell, if it comes with boilerplate script, all the better. Still, open to other input.

ILikePie

  • Bay Watcher
  • Call me Ron
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #162 on: June 03, 2011, 06:58:09 am »

Here's more of a math/physics question. I was reading about additive synthesis and tried to give making a sawtooth or square wave a go.
Here's what I tried doing:
Code: [Select]
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include "ldib/libdib.h"

int main (/*int argc, char** argv/)
{
     int x, y;
     FILE* f = fopen("./bitmap.dib", "w");
     canvas c =  make_canvas(256, 64, WHITE);

     for (x = 0; x < 256; x++) { /* x axis */
      y = 32;
      draw_pixel(x, y, c, (RGB){128, 128, 128} );
     }

     for (x = 0; x < 256; x++) {
      /*
         y = A * sin( wx + p)
         w = 2 * pi * f           */
      y = 32 * sin(2 * M_PI * 110 * x) +
           16 * sin(2 * M_PI * 220 * x) +
           8  * sin(2 * M_PI * 330 * x) +
           4  * sin(2 * M_PI * 440 * x) + 32;
      draw_pixel(x, y, c, BLACK);
     }
     write_bitmap(c, f);
     return 0;
}
If my understanding is correct, I should get a sawtooth wave (or a square wave, I can't remember), but all I got was some messy, two pixel wide line.
« Last Edit: June 03, 2011, 07:16:54 am by ILikePie »
Logged

Virex

  • Bay Watcher
  • Subjects interest attracted. Annalyses pending...
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #163 on: June 03, 2011, 07:08:08 am »

That may be because x is an integer in your code. You can't properly sample a signal with a frequency of more then 400 hz with a measuring interval of 1 second. Try making x a float and measuring with intervals of 1/2000 or even less (sampling theorem says it should be 1/880 at least, but you won't get a pretty line in that case) and adjust your axis to match. Or if you absolutely need x to be an integer, reduce the frequency of your signal.
« Last Edit: June 03, 2011, 07:09:48 am by Virex »
Logged

ILikePie

  • Bay Watcher
  • Call me Ron
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #164 on: June 03, 2011, 07:37:52 am »

Okay, I turned x and y into floats, now the code looks like this:
Code: [Select]
...

int main (int argc, char **argv)
{
     float x, y;
     FILE *f = fopen("./bitmap.dib", "w");
     canvas c =  make_canvas(1024, 128, WHITE);

     for (x = 0; x < 1024; x++) { /* x axis */
      y = 64;
      draw_pixel(x, y, c, (RGB){128, 128, 128} );
     }

     for (x = 0; x < 1024; x += 0.1) {
      /*
         y = A * sin( wx + p )
         w = 2 * pi * f           */
      y = 32 * sin(2 * M_PI * 110 * x) +
           16 * sin(2 * M_PI * 220 * x) +
           8  * sin(2 * M_PI * 330 * x) +
           4  * sin(2 * M_PI * 440 * x) + 64;
      draw_pixel((int)x, (int)y, c, BLACK);
     }
     write_bitmap(c, f);
     return 0;
}
The wave looks funny at the start, then I get a normal sawtooth-ish shape, and then at some point the frequency increases. Decreasing x's increment seems to make that happen faster.
Spoiler: The wave (click to show/hide)
Logged
Pages: 1 ... 9 10 [11] 12 13 ... 91