Bay 12 Games Forum

Please login or register.

Login with username, password and session length
Advanced search  
Pages: 1 ... 6 7 [8] 9 10 ... 91

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

Virex

  • Bay Watcher
  • Subjects interest attracted. Annalyses pending...
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #105 on: May 11, 2011, 01:40:25 pm »

So if anybody here makes games, I need some advice on my design choice. Lets assume I want different types of weapons, and different types of materials for those. So I can have swords, axes and spears for weapons, and bronze, iron and steel for materials. Now these are two ways to implement this. The first is with a class called 'Weapon description' that has a reference to a class called 'Weapon', and each weapon is an object. Likewise there is a class called 'Material' and Bronze is an object of the material class. So for some c# code
Code: [Select]
class WeaponDescription
{
   public string Name {get; }
   public WeaponDescription (string name) //Multiton pattern would be used in the real solution rather then a constructior
   {
      this.Name = name;
   }
}
class Material
{
   public string Name {get; }
   public Material (string name)
   {
      this.Name = name;
   }
}

class Weapon
{
   WeaponDescription weaponDescription;
   Material material;
   public Weapon (WeaponDescription weaponDescription, Material material)
   {
      this.weaponDescription; = weaponDescription;
      this.material = material;
   }
   string Name
   {
      get
      {
         return string.Format("{0} {1}", material.Name, weaponDescription.Name);
      }
   }
}
*Note: Code quickly scrawled in notepad. Not sure to compile, just showing concept.

Or would it be better if each weapon and material was its own class, that extended from their respective higher classes? So I had a sword class and an iron class?
I don't know too much about C#, but my approach would be to pick the simplest possible data structure for a material (structs if possible) and use a class for weapons, since weapons can have all kinds of special properties and functions associated with them, while a material is just providing some values for your functions to work with. Since all materials are alike you'd est store them as an instance of the materials class, with a specifier. If you need special behavior you can always check the specifier from the function defined on the weapon, since it's unlikely that you'll be invoking the function from the material instead of from the weapon
« Last Edit: May 11, 2011, 01:43:51 pm by Virex »
Logged

Max White

  • Bay Watcher
  • Still not hollowed!
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #106 on: May 11, 2011, 06:15:22 pm »

Oh and Max, try to decide if you want to start your membernames with capitals or not, you're now doing both at the same time :)

Public starts with uppercase, private starts with lowercase, static private starts with _
Standard convention these days for c#. So it's not just random between upper and lower case, there is reason to it.

The smart thing for developers to do was to avoid .net in the first place and just use Java, but omg marketing!

... I'll just shed a tear for you, as I know there is little I can do to change you.

I don't know too much about C#, but my approach would be to pick the simplest possible data structure for a material (structs if possible) and use a class for weapons, since weapons can have all kinds of special properties and functions associated with them, while a material is just providing some values for your functions to work with. Since all materials are alike you'd est store them as an instance of the materials class, with a specifier. If you need special behavior you can always check the specifier from the function defined on the weapon, since it's unlikely that you'll be invoking the function from the material instead of from the weapon

Ok thanks. Everybody else I have seen has made classes for specific materials, so I was wondering maybe there is some reason for it, but pure inability to work with objects/structs is also a good explanation. I would also prefer this way because it will allow me to read from a text file while loading up game data, there as making the game moddable DF style.

Siquo

  • Bay Watcher
  • Procedurally generated
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #107 on: May 11, 2011, 06:19:50 pm »

Public starts with uppercase, private starts with lowercase, static private starts with _
Standard convention these days for c#. So it's not just random between upper and lower case, there is reason to it.
Ah, didn't know that, thanks!
Logged

This one thread is mine. MIIIIINE!!! And it will remain a happy, friendly, encouraging place, whether you lot like it or not. 
will rena,eme sique to sique sxds-- siquo if sucessufil
(cant spel siqou a. every speling looks wroing (hate this))

Max White

  • Bay Watcher
  • Still not hollowed!
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #108 on: May 11, 2011, 06:38:40 pm »

Eh, in all reality who ever you work for tends to hold more sway over your naming conventions then anything else. Hell, I have heard of people still having to put the type as a prefix because it was demanded of them, poor souls. But I like standard convention because it makes variable names a little more descriptive as to their nature when your working on a class with a bajillion members and you don't want to keep referring back to a long data dictionary.

olemars

  • Bay Watcher
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #109 on: May 12, 2011, 02:45:38 am »

There must be a million standard conventions. This is the one we use for C++ at work:

Everything is CamelCase
Classes begin with uppercase: class ClassName
Member variables begin with m: mMemberVariable
Static variables begin with s: sStaticVariable
Function begin with lowercase: functionName()
Function parameters begin with a: aParameter
Local variables begin with lowercase: localVariable

I don't see the point of using separate notation for private and public members. Is there something special in C# that warrants it? Underscores make my eyes bleed so I never used them. Particulary hate the Microsoft way of using single and double underscores every bloody where.
Logged

Nadaka

  • Bay Watcher
    • View Profile
    • http://www.nadaka.us
Re: Programming Help Thread (For Dummies)
« Reply #110 on: May 12, 2011, 10:01:54 am »

Yes. there is a reason to use an underscore for privates. Accessing a property uses object.propName instead of object.getPropName() like java, you run into situations where a private member could be accessed with the exact same sequence of characters as the public property if you used a different convention. The underscore is ignored by visual studio when automatically generating getters and setters for a private member.
Logged
Take me out to the black, tell them I ain't comin' back...
I don't care cause I'm still free, you can't take the sky from me...

I turned myself into a monster, to fight against the monsters of the world.

ILikePie

  • Bay Watcher
  • Call me Ron
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #111 on: May 12, 2011, 02:34:16 pm »

I've seen someone use this today "((void (*)())code)()", where code is a char array with a bunch of opcodes (it's supposed to run the opcodes in code). Is it implementation specific? Because I tried calling sig_exit that way, and I got a segfault.

Code: [Select]
$ cat opcode.c
                                         // opcodes were generated using nasm's listing feature
                                         // so I'm pretty sure they aren't wrong

char code[] = "\x66\xB8\x01\x00\x00\x00" // mov  $1, %eax   ; syscall for sys_exit on Linux
     "\x66\xBB\x00\x00\x00\x00"          // mov  $0, %ebx   ; return 0
     "\xCD\x80";                         // int  $0x80      ; execute the system call

int main ()
{
     ((void (*)())code)();
}
$ gcc -o opcode opcode.c
$ ./opcode
Segmentation fault
Logged

Chandos

  • Bay Watcher
  • bork bork bork!
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #112 on: May 12, 2011, 05:40:12 pm »

New question:Does anyone know an easy and simple solution for reading text input within the XNA framework? From what I've read so far on XNA, it's nowhere as straightforward as Windows Forms.
Logged

devek

  • Bay Watcher
  • [KILL_EVERYTHING]
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #113 on: May 12, 2011, 06:19:10 pm »

char code[] = "\x66\xB8\x01\x00\x00\x00" // mov  $1, %eax   ; syscall for sys_exit on Linux
     "\x66\xBB\x00\x00\x00\x00"          // mov  $0, %ebx   ; return 0
     "\xCD\x80";                         // int  $0x80      ; execute the system call


What are you using as an opcode reference? Also, I assuming you are using linux cause for unix operating systems arguments are passed on the stack.

Your code isn't what you think it is.

Code: [Select]
66 b8 01 00 mov ax, 0001
00 00       add [eax], al
66 bb 01 01 mov bx, 0001
00 00       add [eax], al
cd 80       int 80

If you get rid of the 66 prefix, your code should work as intended. You can be more brief than that though, hehe.

Code: [Select]
31 c0       xor eax, eax
40          inc eax
b3 01       mov bl, 01 (it only reads 1 byte for return calls)
cd 80       int 80
Logged
"Why do people rebuild things that they know are going to be destroyed? Why do people cling to life when they know they can't live forever?"

Chandos

  • Bay Watcher
  • bork bork bork!
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #114 on: May 13, 2011, 01:01:25 am »

New question:Does anyone know an easy and simple solution for reading text input within the XNA framework? From what I've read so far on XNA, it's nowhere as straightforward as Windows Forms.

Let me rephrase this: does anyone know of a good GUI support library for XNA 4.0? I've been looking at codeplex.com all day but all the GUI stuff seems to be for 3.x. Any help would be appreciated.
Logged

Max White

  • Bay Watcher
  • Still not hollowed!
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #115 on: May 13, 2011, 01:23:03 am »

xna is not 'what you see is what you get', and as far as I understand there is no support for such a thing. I can't see how it would really benefit the framework for anything but menus, as the 'look' of your window should be defined by the objects it is displaying.

ILikePie

  • Bay Watcher
  • Call me Ron
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #116 on: May 13, 2011, 04:41:21 am »

Spoiler: *snip* (click to show/hide)
I got the opcodes from nasm's listing feature:
Code: [Select]
$ cat code.lst
     1                                  section .text
     2                                      global _start
     3                                  _start:
     4 00000000 66B801000000                mov eax, 1
     5 00000006 66BB00000000                mov ebx, 0
     6 0000000C CD80                        int 80h
I thought 0x66 is mov (it also makes sense that B8 and BB are eax and ebx respectively). Getting rid of the 66 prefix fixes things up:
Code: [Select]
(gdb) disassemble main
Dump of assembler code for function main:
   0x080483b4 <+0>:    push   %ebp
   0x080483b5 <+1>:    mov    %esp,%ebp
   0x080483b7 <+3>:    and    $0xfffffff0,%esp
   0x080483ba <+6>:    mov    $0x804a010,%eax
   0x080483bf <+11>:    call   *%eax
   0x080483c1 <+13>:    mov    %ebp,%esp
   0x080483c3 <+15>:    pop    %ebp
   0x080483c4 <+16>:    ret 
End of assembler dump.
(gdb) disassemble 0x804a010
Dump of assembler code for function code:
   0x0804a010 <+0>:    mov    $0x1,%eax
   0x0804a015 <+5>:    mov    $0x0,%ebx
   0x0804a01a <+10>:    int    $0x80
   0x0804a01c <+12>:    add    %al,(%eax)
End of assembler dump.
Is mov implied when there is no opcode or something?
Logged

devek

  • Bay Watcher
  • [KILL_EVERYTHING]
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #117 on: May 13, 2011, 05:55:03 am »

No, MOV is not implied.

B8 and BB do not stand for eax and ebx, they stand for mov eax and mov ebx.

There are like a hundred different MOV instructions when you count prefixes and addressing. Some people think assembly and machine code are the same thing, they are not. The assembler actually does a lot of work and there are maybe 100 people in the world that know x86 machine code really well. You should be able to download the reference from Intel :) Its like a 1000 page pdf.
Logged
"Why do people rebuild things that they know are going to be destroyed? Why do people cling to life when they know they can't live forever?"

ILikePie

  • Bay Watcher
  • Call me Ron
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #118 on: May 13, 2011, 07:01:13 am »

1000 pages? Wow, someone had a lot of time on their hands.
I kind of figured the multiple MOV opcodes part out when reading through some reference manual, sort of. So if BB and B8 are MOV, what's 66?
Logged

Starver

  • Bay Watcher
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #119 on: May 13, 2011, 08:29:24 am »

Think of it this way.  A single byte that is an opcode can possibly define only 256 different operations (maximum).  The parameters to those opcodes could be absolute values, references to memory locations, references to registers, etc...  Not counting that these absolutes/referenced-tos could be byte-sized, word-sized, etc...

One way this could be done is to make the parameters do some work to by saying "this parameter is an absolute/reference-to-absolute/reference-to-register/etc, of size byte/word/double-word/etc", and that's what the assembler tends to do (straight values, something like "@loc" and keyword references such as "eax", depending on the particular assembler).  But in machine code on all but the simplest chips (that might have only <=256 instruction forms anyway[1]) from the early days you work with multi-byte opcodes which gives the microcode part of the chip the expectation that this to be a move into a register of an absolute value, or of a value from another register, or of a specific memory location... each of these by a different opcode, and many more available, despite being "mov <foo>, <bar>" in the 'human readable' assembler.

That's probably a badly compressed explanation.  and is this Programming Help Thread (For Dummies) any more? :)



[1] I think I already mentioned that I have a conceptual language which needs just four different instructions.  Even at the binary level, it's just two bits.  (All params are references, no need to differentiate any further.)  However, this isn't a 'real' chip's internal language, that I'm aware. :)
Logged
Pages: 1 ... 6 7 [8] 9 10 ... 91