Bay 12 Games Forum

Please login or register.

Login with username, password and session length
Advanced search  
Pages: 1 ... 60 61 [62] 63 64 ... 91

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

Starver

  • Bay Watcher
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #915 on: November 16, 2012, 07:43:27 am »

Zft-!uibu(t!fbtz!fopvhi!up!ep"

'Nmd▼v`x▼nq▼`mnsgdq---(

I often put a bit of catch-code in (even if I'm sure I don't need it) that gives an error, critical or otherwise, when an out-of-range index is going to be used.  It's most useful when instead of going [pseudocode]set var = othervar.object.this.that[12].subitem[4][/pseudocode] you use [pseudocode]set var = GetMeThatValueFor(12,4)[/pseudocode], and in the "GetMeThatValueFor()" function it does the validation and handling before it passes back either the value or a reasonable "this is an invalid value" value, if it's not critical-stop time.

That would include (if also passed, in a more generic handler sub) validation of the "othervar.object.this.that" data reference, because that's also easy to err with.  (Also, depends on whether the parent variable is "global" to both the caller and the handler function, or otherwise OO-linked by inheritance.  IYSWIM.)

edit: Forgot to say.  It's also useful when an out-of-range value is detected to know how out of range it is.  For one thing, you could have gone above the top or below the bottom.  Or, perhaps you somehow doubled 'n' accidentally before trying to reference array[n], but you're normally (supposedly) referencing only the lower half of the array...  When you err on accessing the top item it could be an out-by-one error or the fact that you're trying to go as far beyond again as the maximum index, which is useful to know.  (I'd probably also do an early development/debug stage, while setting up the initial structures, where it gives me a listing of all values in the array, though the chosen method, and that would reveal such an error.)

Also, this applies to both languages that silently allow out-of-rangeness and those that absolutely forbid it.  Because the "This is out of range at line <foo>!" errors are often not worth a damn if your code is in any way non-simple.
« Last Edit: November 16, 2012, 07:54:02 am by Starver »
Logged

thobal

  • Bay Watcher
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #916 on: November 16, 2012, 11:38:27 am »

Zft-!uibu(t!fbtz!fopvhi!up!ep"

'Nmd▼v`x▼nq▼`mnsgdq---(

I often put a bit of catch-code in (even if I'm sure I don't need it) that gives an error, critical or otherwise, when an out-of-range index is going to be used.  It's most useful when instead of going [pseudocode]set var = othervar.object.this.that[12].subitem[4][/pseudocode] you use [pseudocode]set var = GetMeThatValueFor(12,4)[/pseudocode], and in the "GetMeThatValueFor()" function it does the validation and handling before it passes back either the value or a reasonable "this is an invalid value" value, if it's not critical-stop time.

That would include (if also passed, in a more generic handler sub) validation of the "othervar.object.this.that" data reference, because that's also easy to err with.  (Also, depends on whether the parent variable is "global" to both the caller and the handler function, or otherwise OO-linked by inheritance.  IYSWIM.)

edit: Forgot to say.  It's also useful when an out-of-range value is detected to know how out of range it is.  For one thing, you could have gone above the top or below the bottom.  Or, perhaps you somehow doubled 'n' accidentally before trying to reference array[n], but you're normally (supposedly) referencing only the lower half of the array...  When you err on accessing the top item it could be an out-by-one error or the fact that you're trying to go as far beyond again as the maximum index, which is useful to know.  (I'd probably also do an early development/debug stage, while setting up the initial structures, where it gives me a listing of all values in the array, though the chosen method, and that would reveal such an error.)

Also, this applies to both languages that silently allow out-of-rangeness and those that absolutely forbid it.  Because the "This is out of range at line <foo>!" errors are often not worth a damn if your code is in any way non-simple.


that seems like a rather elegant and intelligent way to do things. I'm going to need to rewrite the display code soon because one you get more than a few levels that actually have blocks some serious slowdowns start(mostly because I'm redrawing everyblock, everyframe, even if there is crap in front of it). Otherwise I'm really happy with it right now(mostly because you can rotate 90 degrees if you press Q or E)
Logged
Signature goes here.

Siquo

  • Bay Watcher
  • Procedurally generated
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #917 on: November 16, 2012, 11:57:17 am »

Thobal, have you looked at the code for Stonesense? It manages to display a lot of blocks with reasonable fps by culling them, and the source is on github.
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))

thobal

  • Bay Watcher
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #918 on: November 16, 2012, 01:14:13 pm »

Thobal, have you looked at the code for Stonesense? It manages to display a lot of blocks with reasonable fps by culling them, and the source is on github.

I havnt but I'll check it out. Thanks for the tip.
Logged
Signature goes here.

Starver

  • Bay Watcher
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #919 on: November 16, 2012, 01:25:23 pm »

I don't know if Stonesense does this, but I remember making a basic renderer (for the BBC model B, no less!) that used what I called "unwindowing".  Originally, the visible Window was the whole screen.  Some basic ordering of rendered objects was made according to closeness to the viewpoint (in your case, top-down would work well enough, unless you're ever going for below-the-horizon-upwards viewing) and items rendered in.  As each item goes in, it's tested to see how much (if any) fits within the defined window, gets clipped (if necessary) then what's left is drawn.  Then the Window data is redefined to be "Window minus LatestObject'sFootprint" ready for the next drawing attempt.

(Can't quite remember how I did this on the BBC, without very much in the way of complex data-structures as we have them today (probably just had arrays with ample spare space), but I did it in a way that meant there were always convex areas of "still a Window" defined...  Should be a breeze in more modern languages.  The hardest bits to get right was how to turn a large convex area with a lump chewed out (such that it becomes partially convex) into the most efficient number of smaller convex areas so that later window-tests didn't take too much effort.)

Compared with other methods available to me at the time, it was quite good at what it did, given the hardware involved.  Added to checking the dot-product of facets of objects to ensure I was only even trying to render that faced towards the camera (unless the reverse side of a transparent object... it was mostly triangles, and those that were valid were in turn were 'Unwindowed' if in an opaque construct) it saved a lot of time with saving "rendered surfaces" and comparing each against each new one before clipping (or rejecting) anything new, based upon what was already in front of it.

In a block-based situation, you might be able to shortcut some of that.

However, I've no doubt that renderers (which I use without problem, but rarely program from scratch any more) now have more complex and accurate (yet faster) methods available to them, thanks to a lot of intervening developers and theoreticians.  (And, of course, handing over a lot of the functionality to GPUs that do all this hard work without you having to work it out yourself at all, really.  But I'm a great believer in re-inventing wheels.  And here's a nice square one, so you can park on hills without a handbrake. ;))
Logged

thobal

  • Bay Watcher
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #920 on: November 16, 2012, 02:22:52 pm »

Hmmmmm... Now I just feel like finding a rendering engine that already does this stuff for me.
Logged
Signature goes here.

Starver

  • Bay Watcher
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #921 on: November 16, 2012, 02:39:56 pm »

The hardest bits to get right was how to turn a large convex area with a lump chewed out (such that it becomes partially convex)[...]
...rather "(such that it becomes partially concave)".  Sorry, for the thinko!

(In case you don't understand it, the idea behind this is that it is easier to analyse wither P(x,y) is within a defined concave shape than a convex one.  But I may even have just reduced it to triangular areas, because it's simplicity itself to do the P(x,y) test on Triangle(x1,y1,x2,y2,x3,y3)[1], so I might have just stuck to a (possibly increasing) set of triangles, having started at two triangles that defined the window's entire rectangle.)



[1] You can check P(x,y) against the lines (x1,y1,x2,y2), (x2,y2,x3,y3) and (x3,y3,x1,y1) and see if it lies on the 'inside' of each of them.  If it does that for them all, it's inside that triangle.
Logged

Skyrunner

  • Bay Watcher
  • ?!?!
    • View Profile
    • Portfolio
Re: Programming Help Thread (For Dummies)
« Reply #922 on: November 17, 2012, 01:31:51 am »

Spoiler: preamble (click to show/hide)


ANYHOW, questions:

1. Any guidelines or tips to track down memory leakages?
2. Code:
Code: [Select]
struct plants
  {
    plants();
    ~plants() {delete plantname;}
 
    const string *plantname;
  };
seems to throw the following assert failure:

What is wrong with that?

3. How do you deallocate memory that was inherited from a base class?
Code: [Select]
class baseclass
{
public:
baseclass();
~baseclass();
myClass *pointer;
}

class inheritedClass : public baseclass
{
inheritedClass();
~inheritedClass();
}
In this case, let's say I want to delete the pointer that inheritedClass inherits from baseclass.


I'm considering giving up, or redoing the thing from scratch. Dx It's very confusing and time-consuming.
Logged

bay12 lower boards IRC:irc.darkmyst.org @ #bay12lb
"Oh, they never lie. They dissemble, evade, prevaricate, confoud, confuse, distract, obscure, subtly misrepresent and willfully misunderstand with what often appears to be a positively gleeful relish ... but they never lie" -- Look To Windward

RulerOfNothing

  • Bay Watcher
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #923 on: November 17, 2012, 01:35:53 am »

Well:
For 2, where do you initialise plantname and why is it a pointer to a string?
For 3, the base class' destructor will be called when a derived object is destroyed, so you should free the pointer there.
Logged

Siquo

  • Bay Watcher
  • Procedurally generated
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #924 on: November 17, 2012, 04:55:00 am »

For 4, only destroy stuff that was actually new'ed. Especially deleting an uninitialised pointer is asking for trouble (delete NULL is safe though).
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))

Skyrunner

  • Bay Watcher
  • ?!?!
    • View Profile
    • Portfolio
Re: Programming Help Thread (For Dummies)
« Reply #925 on: November 17, 2012, 07:41:52 am »

plantname is initialized in the constructor, as 0.
And for number three, understood. Dx

I'm pretty sure the delete plantname; is causing #2's errors, as an aside. I comment it out, and it throws #2's error.

Hrm...perhaps plantname is deleted somewhere else. Dx
Logged

bay12 lower boards IRC:irc.darkmyst.org @ #bay12lb
"Oh, they never lie. They dissemble, evade, prevaricate, confoud, confuse, distract, obscure, subtly misrepresent and willfully misunderstand with what often appears to be a positively gleeful relish ... but they never lie" -- Look To Windward

alway

  • Bay Watcher
  • 🏳️‍⚧️
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #926 on: November 18, 2012, 06:27:59 pm »

What I notice about #2:
You don't override the copy constructor.

What this means is that when you create a copy of 'plants,' the pointer is copied by value. When the copy of plants is destroyed, that pointer is destroyed as well. When the original 'plants' or a further copy of it is destroyed, you're then trying to deallocate memory which has already been deallocated.

So something like this would crash:
Code: [Select]
void Foo()
{
   plants myPlant;
   //initialize myPlant's pointer and whatever else needs done here
   Bar(myPlant);
}    //when function ends, myPlant goes out of scope, attempts to delete the thing pointed to
     //(deallocated) and crashes

void Bar(plants myPlant)
{
      //maybe do stuff here
}  //when function ends, local copy of myPlant goes out of scope, deleting the thing pointed to
So you can either manually delete it outside of the destructor or override the copy constructor to create a pointer to a new const string when creating the copy.


Another thing you're trying to do is delete a variable declared as 'const.' Ensure that is created using the 'new' keyword, as if you try something like:
Code: [Select]
const string u = "Hello world";
const string* s = &u;
delete s;
it will also crash, as you're telling a constant variable (u) to change.
« Last Edit: November 18, 2012, 06:34:54 pm by alway »
Logged

Shades

  • Bay Watcher
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #927 on: November 19, 2012, 04:10:12 am »

For #1 Software like valgrind is wonderful for tracking down memory leaks. Although my main advice to avoid memory leaks is avoid using the new keyword (or malloc) and try to prefer references over pointers (or really over smart pointers, then over pointers) it will save you a lot of headaches.

For #2 You probably at least want to check if your pointer is a nullptr before deleting, otherwise, other than I find it weird to delete a const, not sure what else that could be. I assume there is more code than that.

For #3 It depends how you have reference to the object. Assuming B inherits from A:

Code: [Select]
A* foo = new B();
B* bar = new B();

delete bar; // will free up both all memory allocation
delete foo; // will only free up both if A's destructor is virtual
Logged
Its like playing god with sentient legos. - They Got Leader
[Dwarf Fortress] plays like a dizzyingly complex hybrid of Dungeon Keeper and The Sims, if all your little people were manic-depressive alcoholics. - tv tropes
You don't use science to show that you're right, you use science to become right. - xkcd

olemars

  • Bay Watcher
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #928 on: November 19, 2012, 05:49:32 am »

Another thing you're trying to do is delete a variable declared as 'const.' Ensure that is created using the 'new' keyword, as if you try something like:
Code: [Select]
const string u = "Hello world";
const string* s = &u;
delete s;
it will also crash, as you're telling a constant variable (u) to change.

Doing something to a const variable won't cause a crash, at least not from the const itself. It's just a compile-time flag and will at worst cause a compile error (which is its whole purpose in life). Deleting a const is also fine, so
Code: [Select]
const string* s = new string("Hello world");
delete s;
is perfectly acceptable.

Code: [Select]
const string u = "Hello world";
const string* s = &u;
delete s;
is bad since it deletes something that hasn't been newed, as you point out, the const itself is irrelevant. But it is undefined behaviour and might not necessarily crash. Some runtimes silently ignores delete called on stack objects, on others you'll get corruption and potentially a crash, while a few have explicit checks for it (MacOS I believe).

Logged

Urist McScoopbeard

  • Bay Watcher
  • Damnit Scoopz!
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #929 on: November 22, 2012, 11:51:22 am »

So, I have been having a lot of trouble with graphical libraries like Slick and LWJGL, maybe i'm just not learned enough in programming, I'm getting alot of exceptions like I haven't installed them correctly (although I have according to their respective tutorials (outdated?)) and I was wondering if anybody could point to a tutorial/library for 2d games that's relatively easy-to-use. I'm using NetBeans for the record, I've had too much trouble getting stuff to work right on Eclipse, and I learned/am learning on blueJ (which is missing things like packages etc.)

I looked in that other programming thread, but it doesn't have to much info on Java at all.

I've googled a couple of libraries, but none have really stricken my fancy. Help would be greatly appreciated.


EDIT: After about a 1/2 day's worth of troubleshooting I actually managed to get my libs working so thanks anyways (im sure you were all thinking about it)
« Last Edit: November 22, 2012, 04:08:24 pm by Urist McScoopbeard »
Logged
This conversation is getting disturbing fast, disturbingly erotic.
Pages: 1 ... 60 61 [62] 63 64 ... 91