[QUOTE=ROBO_DONUT;25107430]Is it bad that I had to look up what a closure was?[/QUOTE]
It was a joke taken from a Unix koan inspired by those that Eric S. Raymond wrote.
[quote=Master Foo]A student was walking with his master, and asked the question. "Master, which is better, Objects or Closures?" With a smile, the master spake "Objects are a poor man's closures." The student later pondered this in his room. In an effort to impress his Master, he began to study all the functional languages, such as LISP, Clojure and Haskell. It was after mastering these that the student was once again walking with his Master. "Master, I truly understand now. Objects are a poor man's closures." The master struck the student in the forehead with his stick. "Foolish student. When will you learn? Closure's are a poor man's objects!"
Upon hearing this, the student was then enlightened.
[/quote]
[QUOTE=Chandler;25110765]It was a joke taken from a Unix koan inspired by those that Eric S. Raymond wrote.[/QUOTE]
Where are those from, then? Rootless root was kinda awesome, I wanted to read more but there was none :frown:
Not that I really need this at the moment, but out if interest: what's the best way to break out of multiple loops in C++? Would this be the reasonable use of goto to get out of the loops?
That'd probably be the easiest way
Another option is to have a boolean flag that controls all the loops
Set that to false -> all loops break
[img]http://dl.dropbox.com/u/3715122/OOPFail.PNG[/img]
I am coding a logic gate simulator in C# the only problem is I have a come across a structural issue with the way I have done things.
The Form needs to use the logic gate to know how to create it and run the logic gates UpdateInputs/Outputs function. It also stores an array of all the logic gates in the current circuit.
However when it comes to actually wiring these gates up I need to know when a gate is clicked on and this can only be done inside the logic gate class itself and then the logic gate needs to tell the form that a gates been clicked on so it can handle wiring up gate properly. Of course creating a circular dependency.
Any ideas on how to structure this properly I have tried interface classes they don't seem to get me anywhere.
[QUOTE=esalaka;25110834]Where are those from, then? Rootless root was kinda awesome, I wanted to read more but there was none :frown:[/QUOTE]
I found that one mentioned in a comment on HackerNews. They are few and far in between. Maybe you should write some! :v:
[QUOTE=sim642;25111123]Not that I really need this at the moment, but out if interest: what's the best way to break out of multiple loops in C++? Would this be the reasonable use of goto to get out of the loops?[/QUOTE]
Some languages support stuff like "break 2;" (breaks innermost and second to innermost), while others let you assign a "loop label" to a loop and do "break myLoop;". C and C++ don't support anything like this, so the most common thing to do is use goto. This is a bit annoying sometimes when the loop is the last thing of a void-return function, because goto labels have to be followed by a statement (so you have to insert an explicit "return;").
[QUOTE=Chandler;25111724]I found that one mentioned in a comment on HackerNews. They are few and far in between. Maybe you should write some! :v:[/QUOTE]
Surely I am no guru. I have yet to attain enlightment. It is far above me to do such a thing.
[QUOTE=jA_cOp;25111752]Some languages support stuff like "break 2;" (breaks innermost and second to innermost), while others let you assign a "loop label" to a loop and do "break myLoop;". C and C++ don't support anything like this, so the most common thing to do is use goto. This is a bit annoying sometimes when the loop is the last thing of a void-return function, because goto labels have to be followed by a statement (so you have to insert an explicit "return;").[/QUOTE]
Can't you just return; in the loop instead in that case?
[QUOTE=Overv;25111922]Can't you just return; in the loop instead in that case?[/QUOTE]
Sometimes you have three nested for-loops inside a while-loop and you only need to break out of two of the for-loops under certain conditions.
Is there anyway to make it so that a user can use the functions directly?
so say you have:
[CODE]
void sum(int a, int b)
{
cout >> a+b >> endl;
}
[/CODE]
and a user can input:
[QUOTE]sum(5,3)[/QUOTE]
and have the program do its thing?
I guess it would be something like a console
Also incase you hadnt figured out its in C++
Sure. Parse the input as a string till it finds a '('. Then the arguments are separated by a ',' and ends with a ')'. Put them in an array or something.
A std::map, or std::tr1::unordered_map with a string as key and function as value (std::unary_function taking an array returning void for example) would probably be good to hold an array of available functions. Retrieve it and call it.
Wow that was somewhat confusing.
Any examples available?
Too lazy. If you have questions about specific points I'll be glad to answer though.
[QUOTE=ROBO_DONUT;25107430]Is it bad that I had to look up what a closure was?
Low-level? Definitely.
Tedious? Maybe.
Difficult? No. I find it much easier to write than "correct" C++.[/QUOTE]
I would only consider using C with GLib. Raw C involves way too much reinventing of wheels, and dealing with char arrays is a total headache.
[QUOTE=shill le 2nd;25112490]I would only consider using C with GLib. Raw C involves way too much reinventing of wheels, and dealing with char arrays is a total headache.[/QUOTE]
Libraries are for pussies, real men write even the standard library by themselves.
For every project. From scratch.
inb4 butterflies and universal constants
[img]http://imgs.xkcd.com/comics/real_programmers.png[/img]
Real programmers set the universal constants at the start such that the universe evolves to contain the disk with the data they want.
[QUOTE=Overv;25111922]Can't you just return; in the loop instead in that case?[/QUOTE]
Multiple points of return in a function is just asking for that function to evolve into spaghetti code.
[QUOTE=shill le 2nd;25112490]I would only consider using C with GLib. Raw C involves way too much reinventing of wheels, and dealing with char arrays is a total headache.[/QUOTE]
It's not a huge deal. I've written like linked-lists, heaps, and binary trees once, and I copy-paste for every new project.
[QUOTE=ROBO_DONUT;25112913]— — and I copy-paste for every new project.[/QUOTE]
Why don't you compile it to a static library and use that?
[QUOTE=esalaka;25112947]Why don't you compile it to a static library and use that?[/QUOTE]
Because I keep everything in source code. I don't like binary files.
[QUOTE=ROBO_DONUT;25113578]Because I keep everything in source code. I don't like binary files.[/QUOTE]
I thought it'd just be easier to have both - binaries so you can just -lusefulShit and source so you can... Have the source. Or recompile or something.
I have followed a SDL tutorial, I was at the second one, at this page [url]http://lazyfoo.net/SDL_tutorials/lesson02/index.php[/url]
I wrote all the code, compiled it and it gave no errors. Ran it and it terminated with condition 1, and nothing appeared on screen. Then I tried downloading the tutorial's source but that one didn't work either. What is it that's not working? There are 3 "return 1;" in this one, one is when setting up the screen, one when flipping it, one when loading the image. My guess would be the loading one, but I have no idea why its wrong
[cpp]#include "SDL/SDL.h"
#include <string>
//The attributes of the screen
const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;
const int SCREEN_BPP = 32;
SDL_Surface *message = NULL;
SDL_Surface *background = NULL;
SDL_Surface *screen = NULL;
SDL_Surface *load_image( std::string filename )
{
//Temporary storage for the image that's loaded
SDL_Surface* loadedImage = NULL;
//The optimized image that will be used
SDL_Surface* optimizedImage = NULL;
//Load the image
loadedImage = SDL_LoadBMP( filename.c_str() );
//If nothing went wrong with the image
if( loadedImage != NULL )
{
//Create an optimized image
optimizedImage = SDL_DisplayFormat( loadedImage );
//Free the old image
SDL_FreeSurface( loadedImage );
}
//Return the optimized image
return optimizedImage;
}
void apply_surface( int x, int y, SDL_Surface* source, SDL_Surface* destination )
{
//Make a temporary rectangle to hold the offsets
SDL_Rect offset;
//Give the offsets to the rectangle
offset.x = x;
offset.y = y;
//Blit the surface
SDL_BlitSurface( source, NULL, destination, &offset );
}
int main( int argc, char* args[] )
{
//Initialize all SDL subsystems
if( SDL_Init( SDL_INIT_EVERYTHING ) == -1 )
{
return 1;
}
//Set up the screen
screen = SDL_SetVideoMode( SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_SWSURFACE );
//If there was an error in setting up the screen
if( screen == NULL )
{
return 1;
}
//Set the window caption
SDL_WM_SetCaption( "Hello World", NULL );
//Load the images
message = load_image( "hello.bmp" );
background = load_image( "background.bmp" );
//Apply the background to the screen
apply_surface( 0, 0, background, screen );
apply_surface( 320, 0, background, screen );
apply_surface( 0, 240, background, screen );
apply_surface( 320, 240, background, screen );
//Apply the message to the screen
apply_surface( 180, 140, message, screen );
//Update the screen
if( SDL_Flip( screen ) == -1 )
{
return 1;
}
//Wait for 2 seconds
SDL_Delay( 2000 );
//Free the surface
SDL_FreeSurface( message );
SDL_FreeSurface( background );
//Quit SDL
SDL_Quit();
//Return
return 0;
}
[/cpp]
Print some debugging information before each "return 1".
[QUOTE=shill le 2nd;25100546]oh dear god, don't do multiple returns
(actually it probably doesn't matter in a managed environment, but you guessed right, I'm a C/C++ programmer, so it irks me)[/QUOTE]
Why does it even matter which environment you're in? Either way you should make sure to use multiple returns only when it makes sense.
good habit is to create an enumeration and return the messages.
[QUOTE=ROBO_DONUT;25112913]I've written like linked-lists, heaps, and binary trees once, and I copy-paste for every new project.[/QUOTE]
That's a good way to end up with multiple divergent versions of that code, with different sets of bugs and enhancements in each program that uses it. Better to factor it out into a separate library.
I use GLib though. Why maintain these components yourself when you can get a mature, robust implementation for free? If you have unusual memory or performance requirements that's one thing, but for general applications I think a general-purpose library is suitable.
I'll start making a 2d game with XNA.
I wanna do a simple hack'n'slash platformer and I was wondering how I'd actually go for monsters, npcs, the player, etc.
Should I do an entity class and derive a monster class and a player class from it or should I make them both separate classes?
If I manage to do something playable I'll try to add networking, hence my second question, how would I structure entities and stuff, etc...
[QUOTE=Wyzard;25118108]If you have unusual memory or performance requirements that's one thing, but for general applications I think a general-purpose library is suitable.[/QUOTE]
You're telling that to the guy who uses C, a language with a standard library that provides the absolute bare minimum to do any work in. I don't think he minds.
Sorry, you need to Log In to post a reply to this thread.