Bay 12 Games Forum

Please login or register.

Login with username, password and session length
Advanced search  
Pages: 1 ... 114 115 [116] 117 118 ... 795

Author Topic: if self.isCoder(): post() #Programming Thread  (Read 820674 times)

Mego

  • Bay Watcher
  • [PREFSTRING:MADNESS]
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #1725 on: February 26, 2012, 03:49:55 pm »

That methodology is probably better, but I joined a project where the other developer had stuff set up like what I described (minus the global objects being in headers, since there was only one object that was close to being "global", and it was tossed around various functions with pointers), and it made sense to me, so I got hooked on using that setup. It's more of personal preference than believing that my way is the best way.

Gatleos

  • Bay Watcher
  • Mournhold... City of Light... City of MAGIC!
    • View Profile
    • Someone Sig This
Re: if self.isCoder(): post() #Programming Thread
« Reply #1726 on: February 26, 2012, 04:25:54 pm »

Spoiler (click to show/hide)
YUS. Now I just need an engine that can handle the output from this thing.
Logged
Think of it like Sim City, except with rival mayors that seek to destroy your citizens by arming legions of homeless people and sending them to attack you.
Quote from: Moonshadow101
it would be funny to see babies spontaneously combust
Gat HQ (Sigtext)
++U+U++ // ,.,.@UUUUUUUU

Max White

  • Bay Watcher
  • Still not hollowed!
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #1727 on: February 26, 2012, 05:30:27 pm »

Interesting, I did actually not know you could do that, but I think it makes a lot of sense. I usually use the extern keyword to make global objects, it's much better than using singletons in my opinion.
Well that is a rather bold statement. Why do you believe this?

MorleyDev

  • Bay Watcher
  • "It is not enough for it to just work."
    • View Profile
    • MorleyDev
Re: if self.isCoder(): post() #Programming Thread
« Reply #1728 on: February 26, 2012, 07:09:08 pm »

Singletons let you control construction and destruction, but are ugly. Globals usually don't look that clean to me but still look cleaner than singletons, even if they really aren't, and you can't control the construction.

Personally I'll put things that have to be 'global' in their own translation unit in a hidden namespace, and create some global functions to work on it.
Logged

alway

  • Bay Watcher
  • 🏳️‍⚧️
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #1729 on: February 26, 2012, 08:29:45 pm »

Here's some handy code from the 3D space shooter we're working on. It's just about everything needed to do basic Newtonian motion in 3D. It's in Actionscript, but the general ideas apply anywhere.
Spoiler (click to show/hide)
A couple notes on meaning of some called functions and actionscript-isms:
dt is the change in time since the previous update (pretty obvious)
variables are declared with the syntax 'var VarName:VarType' in actionscript
Vector3D is just a wrapper for a 4 element vector, with elements x,y,z,w (in that order)
'Number' is the class for floating point values
Vector.<Number> is a resizeable array of floating point values
MatrixMult has a bunch of handy static function for matrix operations; both multiplying matrices together and matrices multiplied by a Vector3D
rot is a quaternion keeping track of the object's rotation (makes things a hell of a lot easier, learn about quaternion rotations here)
rot.GetMatrix() returns the rotation matrix of the object
the Quatern class takes in the x, y, z orientation of the axis of rotation, and the scalar of magnitude of the rotation around that axis in radians and turns it into a quaternion using the following code:
Spoiler (click to show/hide)
rotMoment.CalcVelocity takes the torque, multiplies it by dt, then divides it by the moment of inertia; in this case, equations from this list of moments of inertia for different shapes of objects. It essentially goes through the equation 'angular acceleration = torque/moment of inertia,' then finds the change in angular velocity by factoring in the change in time.

If you can find a force acting on an object and the place at which it is acting, it does all the rest for you; both motion and rotation

tldr; yay Euler Integration!
« Last Edit: February 26, 2012, 08:51:45 pm by alway »
Logged

Stargrasper

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #1730 on: February 26, 2012, 10:31:17 pm »

Gotta love school...

"Learn C in a week."
"Sure!"

It took all of two days for malloc and pointers to become my two favorite tools for C programming.  Also, I never realized how often I used for loops until I didn't have them.  They aren't in the C89 standard.  They first appear in the C99 standard.
Logged

Mego

  • Bay Watcher
  • [PREFSTRING:MADNESS]
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #1731 on: February 26, 2012, 11:07:39 pm »

In AP CompSci a few weeks back, we had a group activity where we split into two teams, wrote specifications for GridWorld Actors, traded, and developed according to the other team's specs. Naturally, both teams wrote difficult specifications to follow. My team finished ours in 3 days, while the other team just recently got theirs to where it almost works. Our teacher decided to turn the tables on us and make us develop according to our own specifications now. I got frustrated with how slow Java was in doing all the calculations necessary (I made part of the specification include "throws other Actors" and "movement is dictated by Newtonian mechanics"), so I decided to do that part in C++, and somehow interface the two.

tl;dr I was an ass, so I get to make a Newtonian physics engine in C++ and figure out how to make that do the calculations while Java runs Gridworld.

Gotta love school...

"Learn C in a week."
"Sure!"

It took all of two days for malloc and pointers to become my two favorite tools for C programming.  Also, I never realized how often I used for loops until I didn't have them.  They aren't in the C89 standard.  They first appear in the C99 standard.

Luckily, for-loops can be emulated with while-loops fairly easily.

Code: [Select]
for(init; condition; update) {
    // do stuff
}

// is equivalent to

init;
while(condition) {
    // do stuff
    update;
}
« Last Edit: February 26, 2012, 11:09:49 pm by Mego »
Logged

Stargrasper

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #1732 on: February 26, 2012, 11:37:20 pm »

Yeah, I know.  I demonstrated it during my loops lesson.  It's nice, isn't it?  Even better, I'm finding myself really liking C.

You're probably going to want to interface your C++ and Java code with a JNI.  It's not very difficult to do, but it kills portability unless you jump through extra hoops.  And basic physics mechanics aren't really that complicated.  Rule #1: F=ma.

...I should probably write another Java tutorial or a C tutorial one of these days...  I might jump to C for a little while when I give that lesson on using GDB...if only because I've never used it with Java.  Supposedly GDB supports Java, though...
Logged

malloc

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #1733 on: February 27, 2012, 03:08:00 am »

Interesting, I did actually not know you could do that, but I think it makes a lot of sense. I usually use the extern keyword to make global objects, it's much better than using singletons in my opinion.
Well that is a rather bold statement. Why do you believe this?

It's quite simple really.
A singleton comes from the need of having a global module. Often to keep track of some data. It needs to be initialized, and you interface with this module through a function.
You know what you can replace all this boilderplate code with? Global static functions. You get the same functionality, and you just skip all the singleton::GetInstance() interfacing bullshit.

Also, why do you really need to control when to initialize a global object? If it's a global object needed across the program uniformly often, it's going to be needing allocation of start up anyway. Which normal objects do very well. Also something you should notice is that when using singletons, you are actually using dynamic memory allocation. Dynamic memory allocation is slower, as you will be allocating memory multiple times under program start up. Unlike static allocation where the compiler will calculate all the memory needed, and it will be allocated once on program start up. It's probably a neglectable difference in start up time, but it's there.

If you need to control initialization, then it means the object only really need to exist in some specific part of runtime, which you can also do by just allocating a normal object.

Essentially, singletons are just procedural programming wrapped into a complicated objectified interface. You don't have any real need for it, but people still cling to it, because A, it's OOP, and B, it's easy.

Also something about it not working well with parallel programming, but it's not my area of expertise, so I won't comment on that.
Logged

Max White

  • Bay Watcher
  • Still not hollowed!
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #1734 on: February 27, 2012, 03:21:11 am »

You sort of have it the wrong way around, static is a way to use non-OOP methods in a strict OOP language.

Also, database context.

malloc

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #1735 on: February 27, 2012, 03:30:17 am »

That is the point, a singleton is a non OOP pattern. Just wrapped into a object. Yes, I know..
There is no real need for singletons to do databases either.

Anyway, point is I don't use singletons.
Also, I won't judge people for using singletons. The pattern works even it's redundant in many cases.

If you're using them, fine, okay.
Logged

Max White

  • Bay Watcher
  • Still not hollowed!
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #1736 on: February 27, 2012, 03:35:23 am »

You don't use a singleton for your database context... Your code must be some what convoluted.

The point is not only in allowing control over what would otherwise be a static member, it makes the rest of the program agnostic to the instantiation logic of this class, thus conforming to an open and closed principle. It enforces that creational logic is called once and only once when you need it, no matter how much you expand the program.

malloc

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #1737 on: February 27, 2012, 03:48:27 am »

Code: [Select]
//sinplexdatabase.h
#pragma once
#include <map>
#include <string>

class dataentry;
extern std::map<std::string, dataentry> database;

//Utilities to construct or deconstruct objects.
//Or wrap the dataentry object to automatically add itself to the database.

Code: [Select]
//sinplexdatabase.cpp
#include "sinplexdatabase.h"
//define the database object
std::map<std::string, dataentry>database;

Here you have a simplex database object. You just new objects into the database and assign a simple key to this object.

There is one global unique instance of the database.
Not all that convoluted?
Logged

Max White

  • Bay Watcher
  • Still not hollowed!
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #1738 on: February 27, 2012, 03:52:38 am »

Just because you declared it to be static is not the be all and end all. In a much larger program you are going to want to access it with all sorts of things at all sorts of times, including loading data into it, then managing it.

malloc

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #1739 on: February 27, 2012, 06:16:04 am »

Yes of course Max White. You can also do that with global objects.

My only point is that singletons are not strictly speaking necessary for any functionality.
People still use them, that's fine. People also still use the NULL definition. That's also fine, even if it is a little redundant. People also use macros to define constants, which there is also no real need for.

Logged
Pages: 1 ... 114 115 [116] 117 118 ... 795