Bay 12 Games Forum

Please login or register.

Login with username, password and session length
Advanced search  
Pages: 1 ... 18 19 [20] 21 22 ... 91

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

Starver

  • Bay Watcher
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #285 on: September 20, 2011, 09:13:33 am »

(Sorry, started this post ages ago, then got dragged out for dinner with my colleagues before I could post.  Spent well over 2 hours doing absolutely nothing useful, and probably well ninjaed by now...)

Not sure if you're doing this in object-based code or plain functions, or if it's just embedded code blocks just but (if I understand correctly) insert a level in-between the "Display [Here to There]" and the "Give me what is at [ThisPoint]" one that intercepts the out of range and gives you a desired border in its place.

So where you currently have under the Display block/function/object "For ThisPoint = Here..There DO Give me what is at [ThisPoint]" (or however you have it, in whatever form of code it might be), have something (a function, procedure, or just a sanity-checker if/then/else segment) which means you only call the "Give me..." when ThisPoint is valid, but returns an edge-placeholder when it doesn't.

That's a 1-dimensional example, but for you instead of "Draw(ScreenX,ScreenY,Tile[ViewX,ViewY])", or similar, use "Draw(ScreenX,ScreenY,GetTile(ViewX,ViewY))" with the following intermediary function...
Code: [Select]
sub GetTile(in: X,Y) {
  if (X<MinX) or (X>MaxX) then
    if y>0 return StockTile("Air") else return StockTile("Earth")
  else if Y<MinY then return StockTile("Earth")
  else if Y>MaxY then return StockTile("Air")
  else return Tile[X,Y]
}

How (and where) you define your MinX/Ys and which stocktiles (and how they are stored) is up to your code, as well.  Perhaps the "real array" is bordered with the edge tiles you want and juist prepocess with "if X<minX then X=minX" and so on for the other three edge cases.  More complex if it isn't a fully-filled rectangular map (e.g. bits missing, and/or extensions), but that can be sorted if you need it to be.
Logged

Virex

  • Bay Watcher
  • Subjects interest attracted. Annalyses pending...
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #286 on: September 20, 2011, 09:39:46 am »

I haven't got XNA at hand right now. Could you add a break point to see the value of FirstY, it may be that said value is not clamped properly? I've done some quick and dirty calculations that say that everything should be all right, so I'm not sure where the bug comes in.
Logged

qwertyuiopas

  • Bay Watcher
  • Photoshop is for elves who cannot use MSPaint.
    • View Profile
    • uristqwerty.ca, my current (barren) site.
Re: Programming Help Thread (For Dummies)
« Reply #287 on: September 20, 2011, 10:52:44 am »

You could also just write a function that draws an arbitrary rectangle of tiles (in integer units, where 1 unit is one tile), automatically clamping the value to the size of the level, and leaving anything out of bounds blank.

Then, the code for drawing what's on screen would translate the screen coords into the block rectangle coords, which, when passed to that function, can clearly be tested against the level width/height, so only tiles within the bounds of the level are actually drawn.
Logged
Eh?
Eh!

Starver

  • Bay Watcher
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #288 on: September 20, 2011, 11:10:27 am »

(Again, a ninja-post, not yet looked to see if I'm treading on qwerty's toes, though.  Really shouldn't be trying to contribute to a forum while being dragged away from the computer so much. :) )

I haven't got XNA at hand right now. Could you add a break point to see the value of FirstY, it may be that said value is not clamped properly? I've done some quick and dirty calculations that say that everything should be all right, so I'm not sure where the bug comes in.
Reading everything that came in-between when I started my above post and finally pressed the button to finalise it, it made me think that you could have such a "buffer" routine giving (at least during development) a log-file/debug-text-to-screen of all failures to pinpoint things.  Break-points, informative dialogues console/dialogue outputs and log-files are always useful in development, for the more complex sub-sub-sub-sub-sub-procedures you might be using, where you suspect that there might be a GIGO issue beyond what you've originally tested it for. :)  Set it in some output-only code you know you can splice out later without changing functionality, or set it behind a conditional reliant on a global variable set to activate all Development statements either when hard-coded (to be removed later) or from execution parameter that you can set to be ignored in your production code.  (Of course, that way lies the possibility of end-users finding that they can hex-edit/over-patch to re-activate the development code, but what good it does them depends on what internal states it ends up revealing.)


(Also that if you had a complex tile availability issue, e.g. not fully rectangular and completely contiguous values stored in a hash-table, it could returning tile-details unless undef sought tile is undef/equivalent, in which case return a given filler tile which could even, in development, be designed to show what kind of "undefness" it is.  But that's not your situation.)
Logged

Antsan

  • Bay Watcher
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #289 on: September 21, 2011, 11:01:16 am »

I want to invert a matrix in Common Lisp. Although I understand and can apply the Gauß-Jordan algorithm on paper I fail at programming it. I don't know how to handle a matrix of the form
Code: [Select]
0 a b
c d e
0 0 f
as I then just cannot loop from top to bottom, converting the rows, as I would have to start with the second row, continue with the first and finish with the third. So, do I make a list of the numbers of the rows in correct order and loop over that? How would I assemble that list?
Then, what is to do in one step? I am not quite sure... ??? :-\
Logged
Taste my Paci-Fist

Ari Rahikkala

  • Bay Watcher
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #290 on: September 21, 2011, 11:33:47 am »

I wrote a Haskell implementation of Gaussian elimination to understand what's going on in a linear algebra course a little while ago:

Code: [Select]
-- "ref" aka row echelon form
ref [] = []
ref xs | null (head xs) = xs
ref m = next ref (zeroOutBelow $ sortRows m)

-- "sortPre" aka decorate-sort-undecorate
sortPre f = map fst . sortBy (comparing snd) . ap zip (map f)

sortRows = sortPre (length . takeWhile (==0))

next f [] = []
next f (x:xs) =
    x :
    let cols = length $ takeWhile (==0) $ head xs
        (beginSplit, endSplit) = unzip $ map (splitAt cols) xs in
    zipWith (++) beginSplit (f endSplit)

zeroOutBelow [] = []
zeroOutBelow (x:xs) =
    let h = head x in
    x : map (\(line@(l:ls)) ->
                 case l of
                   0 -> line
                   _ -> zipWith (\a b -> b - l / h * a) x line) xs

That is, yes, at each iteration you start by sorting the rows by the number of leading zeroes. Then you multiply down, to zero a column out. The "next" function starts the next iteration in a submatrix that's one row down and as many columns as are zero-filled left, until we're at the empty submatrix and therefore done. This only takes you to row echelon form of course, not the reduced form, but the remaining steps are easy (just do the column-zeroing in bottom-up order, and scale each row)
Logged

Ari Rahikkala

  • Bay Watcher
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #291 on: September 21, 2011, 11:41:42 am »

However, when I get to the 'edge of the level', so to speak, that is to say the last tile in the array, it tried to print one more across, thus giving me an index out of bounds exception.

WWSED?
(What would somebody else do?)

Wrap your level in an interface that substitutes an appropriate "out of bounds" result for any query that's outside of the level bounds. For instance, (using OO terminology) you'd likely have a "isEnterable (coord)" method, or "isWalkable" or whatever you'd like to call it, and it'd always return false when querying outside the level bounds. "getTile" would always return a default or level-specific wall tile or the closest type of wall tile within level bounds, etc.. This is fairly defensive style, and will let you worry less about bounds checks in other parts of the code.
Logged

Virex

  • Bay Watcher
  • Subjects interest attracted. Annalyses pending...
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #292 on: September 21, 2011, 12:12:42 pm »

I want to invert a matrix in Common Lisp. Although I understand and can apply the Gauß-Jordan algorithm on paper I fail at programming it. I don't know how to handle a matrix of the form
Code: [Select]
0 a b
c d e
0 0 f
as I then just cannot loop from top to bottom, converting the rows, as I would have to start with the second row, continue with the first and finish with the third. So, do I make a list of the numbers of the rows in correct order and loop over that? How would I assemble that list?
Then, what is to do in one step? I am not quite sure... ??? :-\
That would work. Store data about each row in a list as a consed pair with the car the row number and the cdr the amount of zeros. You can then sort the list using ":key #'cdr" so it'll sort on the cdr of your pairs. Then you can just loop over that list using


(loop with matrix = (copy-matrix input-matrix)
for row in rowlist
for rownum = (cdr row)   ;Could probably do this on 1 line using do instead of loop, but I hate reading do forms
do (setf matrix (gausstep matrix rownum))
finally (return matrix))

where gausstep is a function that does a single step of the gauss-jordan algorithm and returns the resulting matrix (which I don't know how to implement from the top of my head).
« Last Edit: September 21, 2011, 12:16:59 pm by Virex »
Logged

Max White

  • Bay Watcher
  • Still not hollowed!
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #293 on: October 22, 2011, 02:50:36 am »

Want to make small project. I think I got bored with other games because scope was just too big and I got bored. So, small graphical roguelike game, graphical because right now I am sick of ascii.

Language and libraries*? GO!
*Let's be reasonable, c syntax and OOP please.

Siquo

  • Bay Watcher
  • Procedurally generated
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #294 on: October 22, 2011, 04:26:15 am »

C++ & libtcod too easy?
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 #295 on: October 22, 2011, 04:34:20 am »

Not really... Was thinking something more exotic, but it only just struck me that I never use c++, so might be a good exercise. Gotta keep everything fresh.
Honestly, I was placing my bets that python would be the first to spring up, but guess not!

Edit: Although before I forget, libtcod is ascii isn't it? I swear it is! What am I using for graphics?

ILikePie

  • Bay Watcher
  • Call me Ron
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #296 on: October 22, 2011, 06:25:57 am »

Pretty sure libtcod is graphical. It's used to display ascii characters (or anything else you can think of) out side of the terminal.



Code: [Select]
typedef struct RGB {
     uint8_t red;
     uint8_t green;
     uint8_t blue;
} RGB;

typedef struct canvas {
     int width;
     int height;
     RGB *data;
} canvas;

size_t extract_data_to_canvas (FILE *in, canvas *out)
{
     int width, height, bpp;
     int padding, offset, i, seek;
     size_t len;

     fseek (in, 0xA, SEEK_SET);
     len = fread (&offset, sizeof(uint32_t), 1, in);
     fseek (in, 0x12, SEEK_SET);
     len = fread (&width, sizeof(int32_t), 1, in);
     fseek (in, 0x16, SEEK_SET);
     len = fread (&height, sizeof(int32_t), 1, in);
     fseek (in, 0x1C, SEEK_SET);
     len = fread (&bpp, sizeof(uint16_t), 1, in);

     out->width  = width;
     out->height = height;

     RGB data[width*height];

     if((width * bpp / % 4 == 0)
      padding = 0;
     else
      padding = (4 - ((width * bpp / % 4));

     /* BROKEN */
     seek = 0;
     fseek (in, offset, SEEK_SET);
     for (i = 0; i < width * height; i++, seek += 3) {
      len = fread(&data[i], sizeof(RGB), 1, in);
      if (i > 0 && i % width == 0) {
           seek = seek + padding;
      }
      fseek (in, offset + seek, SEEK_SET);
     }
     out->data = data;

     return len;
}

This one's driving me crazy. Running through out->data's values like this:
Code: [Select]
     for (i = 0; i < width * height; i++)
      printf ("%X %X %X\n", out->data[i].red, out->data.green, out->data.blue);
produces right values. But when calling the function and printing the values again, all I get is random data.
Code: [Select]
...
     extract_data_to_canvas (f, &c);
     for (i = 0; i < c.width * c.height; i++)
      printf ("%X %X %X\n", c.data[i].red, c.data[i].green, c.data[i].blue);
...

I have a feeling I'm not assigning something correctly.
« Last Edit: October 22, 2011, 06:32:26 am by ILikePie »
Logged

Siquo

  • Bay Watcher
  • Procedurally generated
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #297 on: October 22, 2011, 07:10:16 am »

Edit: Although before I forget, libtcod is ascii isn't it? I swear it is! What am I using for graphics?
Well, it uses a tileset, so you can do what DF tilesets do. Otherwise look into SDL or SFML, I guess, I don't know too much about sprite libraries.
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))

RulerOfNothing

  • Bay Watcher
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #298 on: October 22, 2011, 07:13:41 am »

ILikePie, you are actually making out->data point to the same location as the function's data variable, and since that variable is local to the function, the memory it points to is released after the function returns. Therefore you need to copy the data using the memcpy function.
Logged

Siquo

  • Bay Watcher
  • Procedurally generated
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #299 on: October 22, 2011, 07:26:26 am »

And that's why I never use arrays. :(
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))
Pages: 1 ... 18 19 [20] 21 22 ... 91