Set a variable in the innermost loop, break if that variable is set.
[editline]7th June 2013[/editline]
Those are essentially the two solutions.
Goto looks nicer tho.
[editline]7th June 2013[/editline]
[QUOTE=NixNax123;40939872]I thought using goto was bad practice though?[/QUOTE]
Goto is bad practise in general but break is essentially a goto anyway - and it's probably the best-looking solution.
[editline]8th June 2013[/editline]
I remember something about how some people are so desperate to avoid goto that they just use loops that aren't loops (do{}while (0);) and break out of them when they could just use a goto to jump over a part of the code.
I dunno.
If I'm not mistaken, a goto statement translates to a jump in assembly language. Jumps are used all the time in assembly language (any function you make requires them), and therefore the final machinecode will end up with lots of jumps anyway. Jumps are detrimental to performance (or used to be, not sure if they are on modern systems) because the pipeline needs to be flushed before the jump is taken (problematic on long pipeline stages). It's probably done a bit more sophisticatedly within the CPU with microcode nowadays, so goto and jumps aren't a problem in terms of performance. The only reason not to use a goto is that in a lot of cases, if you're using a goto, you're thinking about the solution in the wrong way. In the case of nested loops, goto is fine. Really goto isn't as bad as people claim, if you see it lots in your code then perhaps re-think the solution
[editline]7th June 2013[/editline]
[QUOTE=hogofwar;40905587]I have lots of objects to send over a network (player entities for example), what is the most effecient way of handling them sending from server to client?
Only send the ones where their data has changed?[/QUOTE]
In general, use dead reckoning
[url]http://www.gamasutra.com/view/feature/3230/dead_reckoning_latency_hiding_for_.php[/url]
[QUOTE=Chris220;40928934]Not really, you'll learn by solving it! Feel free to post here if you need a prod in the right direction![/QUOTE]
yeS I'M SO FUCKING HAPPY
i did it without any hints at all omfg yes i feel so accomplished i did it all by myself YES and i got the maximum amount of points
[QUOTE=NixNax123;40939872]I thought using goto was bad practice though?[/QUOTE]
It's just something some dude wrote (with reason). Understand his reasons and you'll understand when it's appropriate to use it or not. Basically, overusing it to rebuild loop constructs makes it harder to follow the flow/state of the program.
[url]http://www.u.arizona.edu/~rubinson/copyright_violations/Go_To_Considered_Harmful.html[/url]
Generally you should always question when someone tells you that doing something is a bad practice. Everything has a context and different things are useful in different situations. Hardly anything shouldn't ever be done/used.
[QUOTE=adnzzzzZ;40943375]It's just something some dude wrote (with reason). Understand his reasons and you'll understand when it's appropriate to use it or not. Basically, overusing it to rebuild loop constructs makes it harder to follow the flow/state of the program.
[url]http://www.u.arizona.edu/~rubinson/copyright_violations/Go_To_Considered_Harmful.html[/url]
Generally you should always question when someone tells you that doing something is a bad practice. Everything has a context and different things are useful in different situations. Hardly anything shouldn't ever be done/used.[/QUOTE]
And the title "Go To Statement Considered Harmful" wasn't even Dijkstra's original title for it, someone changed it for the publication.
But it does really make an important point about how program flow is really hard to follow when there are a lot of jumps.
[QUOTE=esalaka;40943453]And the title "Go To Statement Considered Harmful" wasn't even Dijkstra's original title for it, someone changed it for the publication.
But it does really make an important point about how program flow is really hard to follow when there are a lot of jumps.[/QUOTE]
I just used it once in my program just to make it faster;
[code]#include <stdio.h>
#include <string.h>
#include <math.h>
/* Head ends here */
void displayPathtoPrincess(int n, char grid[n][n]){
/* Get bot position */
...
/* Get princess position */
for(int w=0; w<n; w++) {
for(int h=0; h<n; h++) {
if(grid[w][h] == 'p') {
princess_column = h;
princess_row = w;
goto found;
}
}
}
found:
difference_of_columns = bot_column - princess_column;
difference_of_rows = bot_row - princess_row;
left = right = up = down = 0;
/* Get bot's path and print it */
...
}
}[/code]
Anybody here know how to use Slick2d
Regarding goto:
I've heard somewhere that it makes a compilers job at control flow analysis harder and thus might hinder optimization.
Does anyone know something more specific? I'll try to look something up later if noone does.
[QUOTE=suXin;40935251]I thought about that, but here comes the question 'where to keep that hash?'.
If I'd put it at the beginning or end of file for ease sake, that hash won't be the actual hash of this file.
Or I could check the hash of eveything what's located before the hash at the end of file?[/QUOTE]
Damn why the hell I wanted to take the hash of the entire file if I could just put the hash of the important block. That seems more clean, problem solved.
[QUOTE=suXin;40946994]Damn why the hell I wanted to take the hash of the entire file if I could just put the hash of the important block. That seems more clean, problem solved.[/QUOTE]
A better question might be why are you saving those unimportant things?
Which of the following will this command match and why?
[code]
grep -E "^[^a][bc].*"
abc
Abx
Zc
ZcHello
aBNo
bb
bDbc
bca
cba
bbbbb
[/code]
Not homework, trying to revise for an exam and stuck :|
Cheers :)
Idea: Why don't you try which ones it matches
[editline]8th June 2013[/editline]
Actually, why is it even here? That's hardly programming.
Lol, cheers.
I find myself writing bad code and refactoring too often, so as practice I looked at this conceptual screenshot:
[IMG]http://i.imgur.com/hrDkQ2a.png[/IMG]
and tried describing a decent project layout.
[code]OverworldState, contains:
MapState
Nodes
Parent
Location
SetNode( vector2 position, enum level )
SetNodeParent( parent, child )
MapRenderer
&MapState
Background
Draw()
Draw nodes
Draw backgrounds
Draw ship on map
Draw lines between nodes
ShipState
&MapState
Vector< string playerID > guests
Modules
Location
Type
OnEvent()
IsInFront( vector2 position )
Location
Owner
SetOwner( string playerID )
ShipRenderer
&ShipState
&TileMap
Draw()
Space
Background
Tiles
Modules
Required Classes:
OverworldState
Mapstate
ShipState
Node
ShipModule
Player
Tile
TileMap[/code]
How does that look? Would it work fine when adding new features? Did I miss something bad?
[QUOTE=ZeekyHBomb;40945281]Regarding goto:
I've heard somewhere that it makes a compilers job at control flow analysis harder and thus might hinder optimization.
Does anyone know something more specific? I'll try to look something up later if noone does.[/QUOTE]
Considering function calls are in essence jumps in machine code, I wouldn't have thought a goto would be that detrimental
I am pretty confused with this opengl rendering I am doing. For some reason the thing crashes if I put more than 4 verts in a mesh. It works fine with 4 or less verts. Here is where I create the vertices:
[CODE]
Vertex v1 = new Vertex(-0.1f,0.1f, 0.1f); v1.setRGBA(1.0f, 1.0f, 1.0f, 1.0f); v1.setUV(0.0f, 0.0f); // Top Left (0)
Vertex v2 = new Vertex(0.1f,0.1f, 0.1f); v2.setRGBA(1.0f, 1.0f, 1.0f, 1.0f); v2.setUV(1.0f, 0.0f); // Top Right (1)
Vertex v3 = new Vertex(-0.1f,-0.1f, 0.1f); v3.setRGBA(1.0f, 1.0f, 1.0f, 1.0f); v3.setUV(0.0f, 1.0f); // Bottom Left (2)
Vertex v4 = new Vertex(0.1f,-0.1f, 0.1f); v4.setRGBA(1.0f, 1.0f, 1.0f, 1.0f); v4.setUV(1.0f, 1.0f); // Bottom Right (3)
Vertex v5 = new Vertex(-0.1f, 0.3f, -0.1f); v5.setRGBA(1.0f, 1.0f, 1.0f, 1.0f); v5.setUV(0.0f, 0.0f); // Top Left (4)
Vertex v6 = new Vertex(0.1f, 0.1f, -0.1f); v6.setRGBA(1.0f, 1.0f, 1.0f, 1.0f); v6.setUV(1.0f, 0.0f); // Top Right (5)
Vertex[] newVertices = new Vertex[]{v1, v2, v3, v4, v5, v6};
byte[] newIndices = new byte[]{
0, 1, 2, 3, 4, 5
};
mesh = new Mesh();
mesh.setVertices(newVertices, newIndices);
[/CODE]
In the mesh, the verts and indices are placed in a buffer and then put in a vbo. I have checked and I am 99% sure that this is not where the problem is, all the memory is being allocated correctly and it's all being placed there.
[CODE]
public void setVertices(Vertex[] newVertices, byte[] newIndices){
vertices = newVertices;
indices = newIndices;
vertexBuffer = BufferUtils.createFloatBuffer(40 * vertices.length); // create a vertex buffer
indexBuffer = BufferUtils.createByteBuffer(indices.length); // create the index buffer
// Add the vertices to the vertex buffer
for(int i = 0; i < vertices.length; i++){
vertexBuffer.put(vertices[i].getElements());
}
vertexBuffer.flip();
// Add the indices to the index buffer
indexBuffer.put(indices);
indexBuffer.flip();
// Bind the vbo and fill it with the data from the vertex buffer
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, vertexBuffer, GL_DYNAMIC_DRAW);
// Bind the ebo and fill it with the data from the index buffer
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, indexBuffer, GL_DYNAMIC_DRAW);
}
[/CODE]
And this is where I render. The game crashes right when the glDrawElements is called.
[CODE]
public void render(){
glUseProgram(shaderProgramID); // Use this shader
glBindTexture(GL_TEXTURE_2D, tex.id);
glBindVertexArray(vao); // Bind the vertex attribute array. This is not necessary, but improves performance.
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo); // Bind the element buffer before glDrawElements.
// Reset the model matrix
glIdentityModelMatrix();
// Manipulate the model matrix
glSetScale(scale);
glSetPosition(position);
glSetRotation(rotation);
glSetColor(color);
// Update the matrix
glUpdateModelMatrix();
// Position
glEnableVertexAttribArray(0); // enable the position attribute
glVertexAttribPointer(0, vertices.length, GL_FLOAT, false, 40, 0); // tell opengl how to read the position attribute.
// attribute, amount of elements, type, normalized?, stride, offset ( pointer to first component )
// UVs
glEnableVertexAttribArray(2); // enable the uv attribute
glVertexAttribPointer(2, vertices.length, GL_FLOAT, false, 40, 32); // tell opengl how to read the position attribute.
// Render using the indices
glDrawElements(GL_TRIANGLES, indices.length, GL_UNSIGNED_BYTE, 0);
// cleanup
glUseProgram(0);
glDisableVertexAttribArray(0);
glDisableVertexAttribArray(2);
glBindTexture(GL_TEXTURE_2D, 0);
}
[/CODE]
The game crashes and gives me one of these:
# A fatal error has been detected by the Java Runtime Environment:
#
# EXCEPTION_ACCESS_VIOLATION (0xc0000005) at pc=0x000000005564c360, pid=6448, tid=1852
What do :L
[QUOTE=ZeekyHBomb;40945281]Regarding goto:
I've heard somewhere that it makes a compilers job at control flow analysis harder and thus might hinder optimization.
Does anyone know something more specific? I'll try to look something up later if noone does.[/QUOTE]
I can see why conditional jumps would make flow analysis harder, as you can't necessarily follow them without executing the code - making optimization harder. But in a situation where altering the flow in some way is necessary, I doubt gotos are any worse than other solutions (like function calls, or just other control statements and using state variables).
[QUOTE=jaooe;40949726]Which of the following will this command match and why?
[code]
grep -E "^[^a][bc].*"
abc
Abx
Zc
ZcHello
aBNo
bb
bDbc
bca
cba
bbbbb
[/code]
Not homework, trying to revise for an exam and stuck :|
Cheers :)[/QUOTE]
[^a] is everything but a, [bc] is b or c, . is anything, * is 0 or more times and ^ at the start means it will only match all this if it's at the start of the input string. [not a][b or c][anything else], so... Abx, Zc, ZcHello, bb, bca, cba, bbbbb would be my guess.
[QUOTE=adnzzzzZ;40950945][^a] is everything but a, [bc] is b or c, . is anything, * is 0 or 1 times and ^ at the start means it will only match all this if it's at the start of the input string. [not a][b or c][anything else], so... Abx, Zc, ZcHello, bb, bca, cba, bbbbb would be my guess.[/QUOTE]
BDbc as well apparently.
[QUOTE=hogofwar;40954153]BDbc as well apparently.[/QUOTE]
D is not b or c
I'm still looking into the nodal system in C++, and realized that nodes must have child nodes.
All the galaxies have all the POI's in the galaxy (planets, space stations, etc.) which in turn have all the POI's connected to a galaxy POI( shops in cities, caves, etc. )
Galaxy - Planet - City
The ideal syntax to add child nodes would be something like
[cpp]NodeSystem.AddChild( GalaxyY.PlanetP.CityA, Node );[/cpp]
What'd be the best way to reach that kind of easy-to-use syntax, or is there a better way to do this?
What do you mean by "room for error"? Trying to access something through the operator[] that does not exist? You'd have the same problem if you could overload the operator. (which you currently cannot, although there is a draft and (partial?) implementation of it).
You could return a proxy object, something like
[cpp]struct AstronomicalObjectProxy
{
public:
AstronomicalObjectProxy():Member(nullptr) {}
AstronomicalObjectProxy(PlanetaryObject &object):Member(&object) {}
// make it look like a normal AstronomicalObject
AstronomicalObjectProxy& operator[](const std::string &name) { return Member ? Member[name] : *this; }
bool IsValid() const { return Member; }
AstronomicalObject& Get() { return *Member; }
private:
AstronomicalObject *const Member;
};[/cpp]
This way you can check its validity at the end of your []-chain.
[QUOTE=ZeekyHBomb;40958746]What do you mean by "room for error"? Trying to access something through the operator[] that does not exist? You'd have the same problem if you could overload the operator. (which you currently cannot, although there is a draft and (partial?) implementation of it).
You could return a proxy object, something like
[cpp]struct AstronomicalObjectProxy
{
public:
AstronomicalObjectProxy():Member(nullptr) {}
AstronomicalObjectProxy(PlanetaryObject &object):Member(&object) {}
// make it look like a normal AstronomicalObject
AstronomicalObjectProxy& operator[](const std::string &name) { return Member ? Member[name] : *this; }
bool IsValid() const { return Member; }
AstronomicalObject& Get() { return *Member; }
private:
AstronomicalObject *const Member;
};[/cpp]
This way you can check its validity at the end of your []-chain.[/QUOTE]
That's a very valid way of doing it, but keeping in mind the possibility of a scripting implementation later, wouldn't I be better of doing something along the lines of:
[cpp]
Minimum 1 level of indenting (solar systems)
Maximum 4 levels of indenting (POI's like shops, bars)
AddChild( Galaxy, child ); // solar system
AddChild( Galaxy, Solar System, child ); // planet
AddChild( Galaxy, Solar System, Planet, child ); // city or cave
AddChild( Galaxy, Solar System, Planet, City, child ); // shop or other POI
[/cpp]
?
Why?
It seems more clear for an API or am I saying something weird now :v:
I don't quite get yet why you would want a AstronomicalObjectProxy and wouldn't your implementation allow for only one member?
Unless one city/planet/solar system can belong to multiple planets/solar systems/galaxies I don't see it making sense.
Before you edited your post you said that you worry about some "room for error" when overloading the operator[] with a string as argument.
Like I said in the post, I am not certain what you meant by that and assumed that you worry about some object not being there.
And if that would be the case, in order to just continue the operator[]-chain without having to check each single call you can use the proxy.
[cpp]//without proxy
if(!Galaxy.exist("Solar A"))
return;
auto solar = Galaxy["Solar A"];
if(!solar.exist("Planet A"))
return;
AddChild(solar["Planet A"], node);
//with proxy
AddChild(Galaxy["Solar A"]["Planet A"], node);
//AddChild checks IsValid on the proxy[/cpp]
Yes, sorry for removing that part. Did you mean AstronomicalObject instead of PlanetaryObject in your example?
However, a planet can have multiple cities. What exactly is Member in your example?
Whoops, yep.
I did not (completely) account for cities; I implied that AstronomicalObject is a common ancestor of all the objects you can pass to AddChild.
[editline]9th June 2013[/editline]
If you do not have such a common ancestor, you can resort to a templated proxy-class.
So what your example does, it is simply safety. So when you do something like Galaxy[ "Solar System A" ][ "Planet B" ] it won't end abruply, and you can safely check.
[editline]9th June 2013[/editline]
I'd still need to compare the string received to a list of the object's childs and return the correct child so you can keep traversing it, and if it doesn't match, return a proxy object?
You always return the proxy object, but if the entry is valid you initialize it with the entry (second ctor), otherwise you use the default ctor.
Any particular reason you're not already using a std::map (or something alike) to provide the name -> * mapping?
Sorry, you need to Log In to post a reply to this thread.