Bay 12 Games Forum

Please login or register.

Login with username, password and session length
Advanced search  
Pages: 1 ... 271 272 [273] 274 275 ... 796

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

Max White

  • Bay Watcher
  • Still not hollowed!
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #4080 on: March 05, 2013, 04:27:01 am »

It is funny how somebody can be so happy with a blank screen. I mean it isn't just any blank screen, it is a resizable blank screen that will stretch to any resolution, assuming there is a graphic to draw on it, and there isn't, but if there was, oh boy! It is the most perfect blank screen really... Like making your own canvas to paint on.

Programming java is so fucking zen. I forgot how much it makes you want to grow a beard.

Valid_Dark

  • Bay Watcher
  • If you wont let me Dream, I wont let you sleep.
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #4081 on: March 05, 2013, 04:49:38 am »

Also, I'd like to request a quick tut on vectors, if someone would be so kind.
I need to create a dynamic data array using a user entered value for the size,
And I know this can't be done using arrays, but I've heard some things about vectors.

Here's a documentation. It's rather obtuse, so here's a basic breakdown of all the things you are likely to need in your program:
Code: [Select]
#include <vector>
#include <iostream>

class Type;

Vector<Type> vec();   // A vector that can store objects of type Type. Replace Type with the type of stuff you want the vector to store
Vector<int> intVec();   // A vector of integers
Vector<Vector<bool>> boolVecVec();   // A vector of vector of booleans. Note that Vector<bool> is also a type, as well as Vector<Vector<bool>>
intVec.push_back(3);   // intVec now contains 3
intVec.push_back(5);   // intVec now contains 3 and 5
Vector<float> aFloatVec(4, 123.0f);   // aFloatVec contains 123.0f, 123.0f, 123.0f, and 123.0f
aFloatVec.clear();   // aFloatVec is now empty
for (int i = 0; i < intVec.size(); i++) {
  cout << intVec[i] << endl;
}   // Prints 3 and 5
for (Vector<int>::iterator i = intVec.begin(); i != intVec.end(); i++) {
  cout << *i << endl;
}   // Prints 3 and 5 too

Ok, but those are just 1 dimensional, yeah?
is there an easy way to do 2D vectors, or would it be something like....

cin>> W >> H; //say they put 100 and 100

Vector<int> Data((W*H), 0); // initializes the vector with 0's to establish size

//then access each element with
Data[(y*H)+x];

does that sound about right?

edit: changed from 2 sets of x and y  to x, y, h, w
« Last Edit: March 05, 2013, 05:18:20 am by Valid_Dark »
Logged
There are 10 types of people in this world. Those that understand binary and those that don't


Quote
My milkshake brings all the criminals to justice.

Skyrunner

  • Bay Watcher
  • ?!?!
    • View Profile
    • Portfolio
Re: if self.isCoder(): post() #Programming Thread
« Reply #4082 on: March 05, 2013, 04:54:54 am »

I usually make a class that has the following shape:

class MyDoubleVector
{
public:
// some sort of constructor.
double& at(const unsigned int& x, const unsigned int& y);
private:
vector<double> thisVector;
unsigned int w, h;
double nullvalue;
};

double& MyDoubleVector::at(const unsigned int& x, const unsigned int& y)
{
if (x + y * w < w * h)
return &(thisVector[x + y * w]);
else
return nullvalue;
}


I think I heard that &(thisVector.at()) is illegal outside of MSVC, so no idea how to do it without that :P

edited for optimizing
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

MagmaMcFry

  • Bay Watcher
  • [EXISTS]
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #4083 on: March 05, 2013, 04:56:31 am »

-snop-

Ok, but those are just 1 dimensional, yeah?
is there an easy way to do 2D vectors, or would it be something like....

cin>> X >> Y; //say they put 100 and 100

Vector<int> Data((x*y), 0); // initializes the vector with 0's to establish size

//then access each element with
Data[(y*100)+x]; //note: these aren't the same values as the ones entered  before, but new values used for picking which data you're referring to.

does that sound about right?
That's one way to do it, but you could also do this:
Code: [Select]
cin >> Y >> X;

// Creation:
Vector<Vector<int> > data(Y, Vector<int>(X, 0));

// Access:
data[y][x]
Logged

Max White

  • Bay Watcher
  • Still not hollowed!
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #4084 on: March 05, 2013, 05:01:44 am »

You can always make a class as a thin wrapper for Vector<Vector<Type>>, not 100% sure why you would want to beyond just making code a tiny bit cleaner to read, but you could....

Siquo

  • Bay Watcher
  • Procedurally generated
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #4085 on: March 05, 2013, 05:16:39 am »

You can always make a class as a thin wrapper for Vector<Vector<Type>>, not 100% sure why you would want to beyond just making code a tiny bit cleaner to read, but you could....
Or just make the thinnest wrapper of them all:
typedef std::vector< std::vector< Type > > My2DArray

Don't forget the space between > >, because >> is an operator, and you can initialise easily with the constructors: http://stackoverflow.com/questions/6491251/multi-dimensional-vector-initialization
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: if self.isCoder(): post() #Programming Thread
« Reply #4086 on: March 05, 2013, 05:17:48 am »

That is... Pretty bad ass. Do that one!

Siquo

  • Bay Watcher
  • Procedurally generated
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #4087 on: March 05, 2013, 05:42:54 am »

That said, even though
m_input(100, std::vector< std::vector<int> >(100, std::vector<int>(100) ))
looks cool, making just one vector and accessing it like this
m_input[x+y*100];
as you did it, is recommended (a lot less overhead). It's just harder to resize than the other option.
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: if self.isCoder(): post() #Programming Thread
« Reply #4088 on: March 05, 2013, 05:46:43 am »

Nothing about my own method? :<
* Skyrunner is sad now

Btw, >> is okay in C++11. :D Or I think it is.

Also. Vector of vectors has an advantage of being a jagged array o_O
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

Max White

  • Bay Watcher
  • Still not hollowed!
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #4089 on: March 05, 2013, 05:49:06 am »

Eh, jagged arrays are jagged. Sometimes that is undesirable.
Game worlds that are built from a tile array, for example, you want to be rectangular (Most of the time) thus enforcing that rule with a rectangular array is better.

Siquo

  • Bay Watcher
  • Procedurally generated
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #4090 on: March 05, 2013, 05:52:01 am »

Nothing about my own method? :<
* Skyrunner is sad now
Your method is best method. There, there.  :)

(But seriously, it is, as it abstracts the implementation, so you can change the way it's stored without having to change anything else in the program. Your future colleagues will love you)
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: if self.isCoder(): post() #Programming Thread
« Reply #4091 on: March 05, 2013, 05:55:29 am »

I suppose it could be better with templates for the vector type.
Also, my implementation won't work if you make a vector that isn't rectangular, though the constructor and the private state of the actual vector probably ensures the correctness of the accessor. >_>
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

Siquo

  • Bay Watcher
  • Procedurally generated
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #4092 on: March 05, 2013, 05:58:39 am »

No, but you could add that to the class without having to change anything else. It's the most flexible solution (once it has a more complete interface).
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))

Valid_Dark

  • Bay Watcher
  • If you wont let me Dream, I wont let you sleep.
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #4093 on: March 05, 2013, 05:59:16 am »

Skyrunner, your method was too complex for me. lol.

and I never even considered Jagged arrays, they seem like they could be useful.

so, to sum up,

use vector of vectors when I need a array that changes sizes or is jagged,
and just a super big vector when the size is going to be static, and rectangular (a square is a rectangle) but needs the size to be user defined, and normal arrays for all else.

Logged
There are 10 types of people in this world. Those that understand binary and those that don't


Quote
My milkshake brings all the criminals to justice.

Skyrunner

  • Bay Watcher
  • ?!?!
    • View Profile
    • Portfolio
Re: if self.isCoder(): post() #Programming Thread
« Reply #4094 on: March 05, 2013, 06:04:53 am »

Eh, it's not that complicated.  You just have a class that pretends to be the vector.at() function, but with two coordinates instead of one. The at() does exactly the same thing as vector.at() does, except it checks w & h instead of size() since it's a lot faster. That's important to me because I call at() something like 16400 times a frame. >.> Cellular automata ho.
Also, if you add a method that adds a horizontal row to the vector, it needn't be static that way.
On the other hand, adding a column would be rather complicated.

@Siquo: What else needs to be there? XD Everything vector already does? Like size, an iterator, ...?
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
Pages: 1 ... 271 272 [273] 274 275 ... 796