• What do you need help with? Version 5
    5,752 replies, posted
  • [QUOTE=LaughingStock;37183100]Alright that makes sense now. I'm still not sure what they're for, do they allow you to create multiple instances of something? Also, do you know if there are any sites with more examples and exercises?[/QUOTE] Lua is not a class based language and therefore doesn't have instances. Stateless iterators are purely for iterating.
  • [QUOTE=muslim obama;37188776]How do I compile and link a .c file using vim in linux, so that I can execute it?[/QUOTE] Yay a fellow Linux dev! Add me on steam if you want direct help and tips, for that particular question you can easily just write a command like [code]:!gcc -o main main.c test.c blah.c[/code] but you should really to look into [url=http://mrbook.org/tutorials/make/]makefiles[/url], it's a more standard way of compiling your projects. Vim has a handy command "make" (or "mak" for short) that runs the local makefile; then lets you zoom to errors or warnings that the compiler produces. I have more tips on how to make your life easier! Add me as a friend on steam as posting them all on here would be lengthy.
  • I have a problem with my Visual Studio. I recently switched from Visual Basic to C#. When I try to inherit from an other class in C#, I can't see the Methods I can override. In Visual Basic I could simply choose a Method of the Base-Class from a Dropdown-Menu I wanted to override. Is this possible in Visual C#, too?
  • [QUOTE=monky2k6;37189304]I have a problem with my Visual Studio. I recently switched from Visual Basic to C#. When I try to inherit from an other class in C#, I can't see the Methods I can override. In Visual Basic I could simply choose a Method of the Base-Class from a Dropdown-Menu I wanted to override. Is this possible in Visual C#, too?[/QUOTE] Type in "public override " and intellisense brings up a list of methods in the base class that can be overridden (has the "virtual" keyword)
  • Do you guys recommend that I further my knowledge of openGL (I've gotten to the end of what's finished of open.gl) or apply and practice my current knowledge until Overv releases the next part?
  • [QUOTE=Meatpuppet;37190223]Do you guys recommend that I further my knowledge of openGL (I've gotten to the end of what's finished of open.gl) or apply and practice my current knowledge until Overv releases the next part?[/QUOTE] Guess you just got ninja'd.
  • :) thanks a whole lot everyone here for not being a dick to me when I ask questions that seem stupid and especially Overv for making the fantastic tutorial!
  • [B]Edit:[/B]I literally figured out the problem as soon as I posted this.
  • Ok so I made this little random dice roller. You enter in a high number (such as 20), and it will pick a number from 1 to 20. I even set it up so that it tells you if you roll a critical or not. Anyway, I want to ask about entering in things that aren't numbers. If I enter in a letter or symbol, the program goes haywire and starts spewing stuff out. How do I prevent people from entering in things they shouldn't? Also it's in C++ [CODE] #include <iostream> #include <cstdlib> #include <time.h> using namespace std; int dice_rnd(int low, int high) { return (rand() % high + 1); } int main() { srand ( time(NULL) ); int dice_low, dice_high, dice_outcome; dice_low = 0; while(true) { cout << "Roll: "; cin >> dice_high; dice_outcome = dice_rnd(dice_low, dice_high); if (dice_outcome == dice_high) { cout << dice_outcome << " CRITIAL!\n"; } if (dice_outcome == 1) { cout << dice_outcome << " CRITIAL FAIL!\n"; } else { cout << dice_outcome << "\n"; } } return 0; } [/CODE]
  • Grab a line and parse it yourself, check to see if the parsing failed so you can slap the user silly. Unfortunately I don't know of an easy pure C++ way of doing this; here's an example using sscanf: [code]std::string Line; getline(std::cin,Line); float Input; int NumberOfParses = sscanf(Line.c_str(),"%f",&Input); if (NumberOfParses != 1) { std::cout << "You twat, that's not a number.\n"; } else { std::cout << "Yay, you inserted a number!\n"; }[/code]
  • Could you maybe put that into simple terms for me? I'm still quite new to C++ :v:
  • [QUOTE=slayer20;37193234]Could you maybe put that into simple terms for me? I'm still quite new to C++ :v:[/QUOTE] well im an idiot and also dont know c++ and this is probably worst way possible but cant you just put try - catch in dice_rnd function ? you can try rand() % high + 1 and if it fails return something else
  • I wrote you a simple example to show you how you could do it: [cpp]#include <iostream> int main() { int num; std::cout << "Enter a number: "; // When you use cin to pipe a value into a variable, it can return true or false. // In this case, if we try to put a non-number value into 'num', it will return // a value of false, so the while loop will execute its block of code while(!(std::cin >> num)) { std::cout << "That isn't a number!" << std::endl; // These two lines discard whatever might be left over in std::cin std::cin.clear(); std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); std::cout << "Enter a number: "; } // If we ever break out of the while loop, it's because the user entered a number std::cout << "The number you entered was: " << num << std::endl; return 0; }[/cpp]
  • [QUOTE=Chris220;37194919]I wrote you a simple example to show you how you could do it: [cpp]#include <iostream> int main() { int num; std::cout << "Enter a number: "; // When you use cin to pipe a value into a variable, it can return true or false. // In this case, if we try to put a non-number value into 'num', it will return // a value of false, so the while loop will execute its block of code while(!(std::cin >> num)) { std::cout << "That isn't a number!" << std::endl; // These two lines discard whatever might be left over in std::cin std::cin.clear(); std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); std::cout << "Enter a number: "; } // If we ever break out of the while loop, it's because the user entered a number std::cout << "The number you entered was: " << num << std::endl; return 0; }[/cpp][/QUOTE] Yours handles most of the cases, but not all. When user inputs "123asd" then your code would happily get 123 as number and leave the "asd" part hanging in input buffer that would get read the next time you ask for some input. I once wrote a custom input function for this purpose, which also checks if all input got parsed properly to the end: [cpp]#include <iostream> #include <limits> using namespace std; template<typename T> bool valid_input(istream &stream, T &value) { bool good = true; stream >> value; if (!stream || stream.peek() != '\n') // checks if value was parsed correctly and all input was parsed too { good = false; } if (!good) // stream gets reset if parsing went bad { stream.clear(); stream.ignore(numeric_limits<streamsize>::max(), '\n'); } return good; } int main() { int n; while (cout << "Enter a number: ", !valid_input(cin, n)) // nifty way to use this function together with input prompt { cout << "Not a number!" << endl; } cout << "Your number was " << n << endl; return 0; } [/cpp]
  • [QUOTE=sim642;37195052]Yours handles most of the cases, but not all. When user inputs "123asd" then your code would happily get 123 as number and leave the "asd" part hanging in input buffer that would get read the next time you ask for some input. I once wrote a custom input function for this purpose, which also checks if all input got parsed properly to the end: <code snorp>[/QUOTE] Very true, thanks for pointing that out.
  • As part of my wanting to expand my C# knowledge im diving into OGL with OpenTK, I have attempted to re-write some of my utility classes in C# but my FBO class gives me this error, I remember OGL giving an error in C++ when I tried to create an FBO but it would never crash. This is the error: [code]mono ./OpenTKGaem.exe Unhandled Exception: OpenTK.Graphics.GraphicsErrorException: InvalidOperation at OpenTK.Graphics.OpenGL.ErrorHelper.CheckErrors () [0x00000] in <filename unknown>:0 at OpenTK.Graphics.OpenGL.ErrorHelper.Dispose () [0x00000] in <filename unknown>:0 at OpenTK.Graphics.OpenGL.GL.FramebufferRenderbuffer (FramebufferTarget target, FramebufferAttachment attachment, RenderbufferTarget renderbuffertarget, UInt32 renderbuffer) [0x00000] in <filename unknown>:0 at OpenTKGaem.GLUtilities.FBO.Create (Int32 w, Int32 h) [0x00000] in <filename unknown>:0 at OpenTKGaem.Game.OnLoad (System.EventArgs e) [0x00000] in <filename unknown>:0 at OpenTK.GameWindow.OnLoadInternal (System.EventArgs e) [0x00000] in <filename unknown>:0 at OpenTK.GameWindow.Run (Double updates_per_second, Double frames_per_second) [0x00000] in <filename unknown>:0 at OpenTK.GameWindow.Run (Double updateRate) [0x00000] in <filename unknown>:0 at OpenTKGaem.MainClass.Main (System.String[] args) [0x00000] in <filename unknown>:0 [ERROR] FATAL UNHANDLED EXCEPTION: OpenTK.Graphics.GraphicsErrorException: InvalidOperation at OpenTK.Graphics.OpenGL.ErrorHelper.CheckErrors () [0x00000] in <filename unknown>:0 at OpenTK.Graphics.OpenGL.ErrorHelper.Dispose () [0x00000] in <filename unknown>:0 at OpenTK.Graphics.OpenGL.GL.FramebufferRenderbuffer (FramebufferTarget target, FramebufferAttachment attachment, RenderbufferTarget renderbuffertarget, UInt32 renderbuffer) [0x00000] in <filename unknown>:0 at OpenTKGaem.GLUtilities.FBO.Create (Int32 w, Int32 h) [0x00000] in <filename unknown>:0 at OpenTKGaem.Game.OnLoad (System.EventArgs e) [0x00000] in <filename unknown>:0 at OpenTK.GameWindow.OnLoadInternal (System.EventArgs e) [0x00000] in <filename unknown>:0 at OpenTK.GameWindow.Run (Double updates_per_second, Double frames_per_second) [0x00000] in <filename unknown>:0 at OpenTK.GameWindow.Run (Double updateRate) [0x00000] in <filename unknown>:0 at OpenTKGaem.MainClass.Main (System.String[] args) [0x00000] in <filename unknown>:0[/code] And while im at it if anyone can tell me if there is anything I could improve with this code: [csharp]using System; using OpenTK; using OpenTK.Graphics.OpenGL; namespace OpenTKGaem.GLUtilities { public class FBO { uint fbo; uint depthBuff; uint texture; public FBO () { GL.GenRenderbuffers(1, out depthBuff); GL.GenTextures(1, out texture); GL.GenFramebuffers(1, out fbo); } ~FBO () { GL.DeleteTextures( 1, ref texture ); GL.DeleteRenderbuffers(1, ref depthBuff); GL.DeleteFramebuffers( 1, ref fbo ); } public bool Create(int w, int h) { GL.BindRenderbuffer(RenderbufferTarget.RenderbufferExt, depthBuff); GL.RenderbufferStorage(RenderbufferTarget.RenderbufferExt,RenderbufferStorage.DepthComponent16, w, h); GL.FramebufferRenderbuffer(FramebufferTarget.FramebufferExt, FramebufferAttachment.DepthAttachmentExt, RenderbufferTarget.RenderbufferExt, depthBuff); GL.BindTexture(TextureTarget.Texture2D, texture); GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba, w, h, 0, PixelFormat.Rgba, PixelType.UnsignedByte, IntPtr.Zero); GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapS, (int)TextureParameterName.ClampToEdge); GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapT, (int)TextureParameterName.ClampToEdge); GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Linear); GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.Linear); GL.BindTexture(TextureTarget.Texture2D, 0); GL.BindFramebuffer(FramebufferTarget.FramebufferExt, fbo); GL.FramebufferTexture2D(FramebufferTarget.FramebufferExt, FramebufferAttachment.ColorAttachment0Ext, TextureTarget.Texture2D, texture, 0); GL.FramebufferRenderbuffer(FramebufferTarget.FramebufferExt, FramebufferAttachment.DepthAttachmentExt, RenderbufferTarget.RenderbufferExt, depthBuff); FramebufferErrorCode status = GL.CheckFramebufferStatus(FramebufferTarget.FramebufferExt); if (status != FramebufferErrorCode.FramebufferCompleteExt) { System.Console.WriteLine("Couldn't create frame buffer"); GL.BindFramebuffer(FramebufferTarget.FramebufferExt, 0); return false; } GL.BindRenderbuffer(RenderbufferTarget.RenderbufferExt, 0); GL.BindFramebuffer(FramebufferTarget.FramebufferExt, 0); return true; } public void SetActive(bool setActive = true) { if(setActive) GL.BindFramebuffer(FramebufferTarget.FramebufferExt, fbo); else GL.BindFramebuffer(FramebufferTarget.FramebufferExt, 0); } public uint GetTexture() { return texture; } public void Clear(float r = 0.0f, float g = 0.0f, float b = 0.0f, float a = 1.0f) { this.SetActive(true); GL.ClearColor(r, g, b, a); GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit); } } } [/csharp] Oh btw in C++ I would use null as te last argument of TexImage2d but C# doesnt let me do this so I used IntPtr.Zero GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba, w, h, 0, PixelFormat.Rgba, PixelType.UnsignedByte, IntPtr.Zero);
  • You can't call GL functions from the destructor, it's executed on a different thread. No idea what causes this error though. [editline]13th August 2012[/editline] Maybe there's more info in the Exception object, I think OpenTK includes the error message somewhere.
  • [QUOTE=Richy19;37205644]Ahh shit, how could I do the automatic deleting of OpenGL Resources?[/QUOTE] glDelete?
  • [QUOTE=Meatpuppet;37205700]glDelete?[/QUOTE] Yes, but if I cant call it in the destructor then is there any other method whereby I dont have to call a custom function myself to delete it all.
  • [QUOTE=Richy19;37205933]Yes, but if I cant call it in the destructor then is there any other method whereby I dont have to call a custom function myself to delete it all.[/QUOTE] I have an Action queue that is filled with lambda closures from by the destructors and emptied by a function called in the main loop. [csharp] ~Texture() { RenderingMethodQueue.Enqueue(() => { GL.DeleteTexture(_textureId); Trace.TraceDeleteTexture(_name); }); } [/csharp] Something like that. No idea if it's thread-safe. The destructors are also cancelled after a few seconds, so it's important to avoid long running code.
  • [QUOTE=Richy19;37205933]Yes, but if I cant call it in the destructor then is there any other method whereby I dont have to call a custom function myself to delete it all.[/QUOTE] You could (maybe) make a list with glNewList() and put all your resources in there, then clear resources with glDeleteLists()
  • [QUOTE=Richy19;37205644]Ahh shit, how could I do the automatic deleting of OpenGL Resources?[/QUOTE] The way I do it is that I make all OpenGL "objects" (anything that needs to glGen* and glDelete*) implement IDisposable [url=http://msdn.microsoft.com/en-us/library/ms244737(v=vs.80).aspx](make sure to implement it correctly)[/url], and in debug mode I throw an exception when it's not properly disposed. I'm actually planning on replacing this with a better system later on, but it very effectively lets us know when we're leaking OpenGL resources. [csharp] private void Dispose(bool disposing) { if (!disposed) { if (disposing) { if (Exists) { GL.DeleteBuffers(1, ref id); id = 0; } } disposed = true; #if DEBUG if (id != 0) throw new Exception("VBO not disposed properly"); #endif } } [/csharp]
  • [QUOTE=robmaister12;37209302]The way I do it is that I make all OpenGL "objects" (anything that needs to glGen* and glDelete*) implement IDisposable [url=http://msdn.microsoft.com/en-us/library/ms244737(v=vs.80).aspx](make sure to implement it correctly)[/url], and in debug mode I throw an exception when it's not properly disposed. I'm actually planning on replacing this with a better system later on, but it very effectively lets us know when we're leaking OpenGL resources. [csharp] private void Dispose(bool disposing) { if (!disposed) { if (disposing) { if (Exists) { GL.DeleteBuffers(1, ref id); id = 0; } } disposed = true; #if DEBUG if (id != 0) throw new Exception("VBO not disposed properly"); #endif } } [/csharp][/QUOTE] Does C# automatically call Dispose for you or do you put all your resources nto a list and dispose of them all at the end? Using this method tho, wouldnt leaving the dispose call to the destructor make it leak the resources? [editline]13th August 2012[/editline] This is how i have it now [csharp] public class FBO : IDisposable { uint fbo; uint depthBuff; uint texture; bool disposed; public FBO () { GL.GenRenderbuffers (1, out depthBuff); GL.GenTextures (1, out texture); GL.GenFramebuffers (1, out fbo); disposed = false; } public void Dispose () { Dispose (true); //GC.SuppressFinalize(this); } ~FBO () { Dispose (false); } protected virtual void Dispose (bool disposing) { if (!disposed) { if (disposing) { if (texture != 0) GL.DeleteTextures (1, ref texture); if (depthBuff != 0) GL.DeleteRenderbuffers (1, ref depthBuff); if (fbo != 0) GL.DeleteFramebuffers (1, ref fbo); fbo = 0; depthBuff = 0; texture = 0; } disposed = true; if (fbo != 0) throw new Exception ("FrameBuffer not disposed properly"); if (depthBuff != 0) throw new Exception ("DepthBuffer not disposed properly"); if (texture != 0) throw new Exception ("Texture not disposed properly"); } }[/csharp]
  • [QUOTE=Richy19;37210956]Does C# automatically call Dispose for you or do you put all your resources nto a list and dispose of them all at the end?[/QUOTE] Usually it's used with the [URL="http://msdn.microsoft.com/en-gb/library/yh598w02%28v=vs.100%29.aspx"]using statement[/URL]. Dispose is also (explicitly) called from the finalizer in most cases, but that wouldn't work for OpenGL. The only options are manual resource management or some kind of queue or collector that runs on the main thread. (Or possibly a hack involving shared contexts if you can delete textures created in the other context. I don't know enough about OpenGL to know whether that would work.)
  • yea just created a list and run it in the background at the end of the program. Next up im trying to write the shader code: from this: [cpp] bool Shader::linkShader() { GLint Result = GL_FALSE; int InfoLogLength; /// Link the program //fprintf(stdout, "Linking program "); glAttachShader(shaderID, VertexShaderID); glAttachShader(shaderID, FragmentShaderID); glLinkProgram(shaderID); /// Check the program glGetProgramiv(shaderID, GL_LINK_STATUS, &Result); glGetProgramiv(shaderID, GL_INFO_LOG_LENGTH, &InfoLogLength); std::vector<char> ProgramErrorMessage( glm::max(InfoLogLength, int(1)) ); glGetProgramInfoLog(shaderID, InfoLogLength, NULL, &ProgramErrorMessage[0]); if(Result == GL_FALSE) fprintf(stdout, "%s\n", &ProgramErrorMessage[0]); if(Result = GL_TRUE) return true; else return false; }[/cpp] to [csharp] bool linkShader () { int Result; int InfoLogLength; /// Link the program //fprintf(stdout, "Linking program "); GL.AttachShader(shaderID, VertexShaderID); GL.AttachShader(shaderID, FragmentShaderID); GL.LinkProgram(shaderID); /// Check the program GL.GetProgram(shaderID, ProgramParameter.LinkStatus, Result); GL.GetProgram(shaderID, ProgramParameter.InfoLogLength, InfoLogLength); if(Result = true) return true; else return false; } [/csharp] but how do I do all the error checking that is done in the C++ version [editline]13th August 2012[/editline] Anyone mind having a look and tell me what/if I can improve? oh and if it works with the fbo on your computer [url]http://dl.dropbox.com/u/18453712/OpenTKGaem.zip[/url]
  • For an android game GUI, should I use the built in buttons and stuff, or should I just make my own bounding boxes checking for where the pointers are and stuff like that?
  • [I]GL.GetProgramInfoLog(int program)[/I] returns the info log as string. [csharp]if(Result = true) return true;[/csharp] This part doesn't compile. You can't compare or assign bools to int variables. (It's also wrong in the C++ code, it always returns true.) You should probably just throw an exception if the program isn't linked properly and change the method to void. Using error codes is very uncommon in .NET, the only place it's used in the framework that I know of is the Dictionary's TryGetValue method.