Jump to content

  • Log In with Google      Sign In   
  • Create Account

Interested in a FREE copy of HTML5 game maker Construct 2?

We'll be giving away three Personal Edition licences in next Tuesday's GDNet Direct email newsletter!

Sign up from the right-hand sidebar on our homepage and read Tuesday's newsletter for details!


We're also offering banner ads on our site from just $5! 1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


kobuscrispi

Member Since 14 Jan 2006
Offline Last Active Nov 06 2012 10:26 AM

Topics I've Started

Rendering text using OpenTK

03 November 2012 - 03:48 PM

In the early stages of working on a game project using Visual Studio 2012 and OpenTK. What I'm working on right now, based on a series of tutorials I found, is a way to render text reliably. The general method I'm using is to create a Bitmap, draw the text to the Bitmap, then use it as a GL texture. The current method seems pretty inefficient (I'm recreating the bitmap every frame, I could probably split it into one smaller Bitmap per string and not have to constantly redraw them), but the problem I'm having is before that. After a few seconds of generating bitmaps, the program crashes.

Code:
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Text;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using OpenTK;
using OpenTK.Graphics.OpenGL;
using OpenTK.Input;
namespace FusionMan {
	class TextObject {
		public string text;
		public PointF pos;
		public TextObject(string nText, PointF nPos) {
			text = nText;
			pos = nPos;
		}
	}
	class Controller {
		Bitmap textBmp;
		int textTexture;
		Font font;
		List<TextObject> textObjects;
		// Called in OnLoad
		public void initialize() {
			// Initialize text rendering
			// Create Bitmap and OpenGL texture
			textBmp = new Bitmap(640, 480); // match window size
			PrivateFontCollection fontCollection = new PrivateFontCollection();
			fontCollection.AddFontFile("fixed.ttf");
			font = new Font(
				fontCollection.Families[0],
				11,
				FontStyle.Regular);
			flushText();
		}
		private void flushText() {
			textObjects = new List<TextObject>();
		}
		public void addText(TextObject textObject) {
			textObjects.Add(textObject);
			textBmp.Dispose();
			textBmp = new Bitmap(640, 480); // match window size
			textTexture = GL.GenTexture();
			GL.BindTexture(TextureTarget.Texture2D, textTexture);
			GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)All.Linear);
			GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)All.Linear);
			GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba, textBmp.Width, textBmp.Height, 0,
				OpenTK.Graphics.OpenGL.PixelFormat.Bgra, PixelType.UnsignedByte, IntPtr.Zero); // just allocate memory, so we can update efficiently using TexSubImage2D
			Graphics gfx = Graphics.FromImage(textBmp);
			gfx.TextRenderingHint = TextRenderingHint.ClearTypeGridFit;
			foreach (TextObject t in textObjects) {
				gfx.DrawString(t.text, font, Brushes.White, t.pos);
			}
			BitmapData data = textBmp.LockBits(new Rectangle(0, 0, textBmp.Width, textBmp.Height), ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
			GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba, 640, 480, 0,
				OpenTK.Graphics.OpenGL.PixelFormat.Bgra, PixelType.UnsignedByte, data.Scan0);
			textBmp.UnlockBits(data);
		}
		private void drawText() {
			GL.LoadIdentity();
			GL.Ortho(0.0, 640.0, 0.0, 480.0, 0.0, 4.0);
			GL.Enable(EnableCap.Texture2D);
			GL.Enable(EnableCap.Blend);
			GL.BlendFunc(BlendingFactorSrc.One, BlendingFactorDest.OneMinusSrcAlpha);
			GL.Color3(Color.White);
			GL.Begin(BeginMode.Quads);
			GL.TexCoord2(0f, 1f); GL.Vertex2(0f, 0f);
			GL.TexCoord2(1f, 1f); GL.Vertex2(640f, 0f);
			GL.TexCoord2(1f, 0f); GL.Vertex2(640f, 480f);
			GL.TexCoord2(0f, 0f); GL.Vertex2(0f, 480f);
			GL.End();
		}
		// Called in OnRenderFrame
		public void draw() {
			flushText();
			addText(new TextObject("Test", new PointF(10, 360)));
			drawText();
		}
	}
}

Error message:

System.ArgumentException was unhandled
  HResult=-2147024809
  Message=Parameter is not valid.
  Source=System.Drawing
  StackTrace:
	   at System.Drawing.Bitmap..ctor(Int32 width, Int32 height, PixelFormat format)
	   at System.Drawing.Bitmap..ctor(Int32 width, Int32 height)
	   at FusionMan.Controller.addText(TextObject textObject) in c:\Users\algordon\Documents\Visual Studio 2012\Projects\FusionMan\FusionMan\Controller.cs:line 50
	   at FusionMan.Controller.draw() in c:\Users\algordon\Documents\Visual Studio 2012\Projects\FusionMan\FusionMan\Controller.cs:line 88
	   at FusionMan.SimpleWindow.OnRenderFrame(FrameEventArgs e) in c:\Users\algordon\Documents\Visual Studio 2012\Projects\FusionMan\FusionMan\Program.cs:line 61
	   at OpenTK.GameWindow.OnRenderFrameInternal(FrameEventArgs e)
	   at OpenTK.GameWindow.RaiseRenderFrame(Stopwatch render_watch, Double& next_render, FrameEventArgs render_args)
	   at OpenTK.GameWindow.DispatchUpdateAndRenderFrame(Object sender, EventArgs e)
	   at OpenTK.GameWindow.Run(Double updates_per_second, Double frames_per_second)
	   at FusionMan.SimpleWindow.Main() in c:\Users\algordon\Documents\Visual Studio 2012\Projects\FusionMan\FusionMan\Program.cs:line 72
	   at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
	   at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
	   at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
	   at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
	   at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
	   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
	   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
	   at System.Threading.ThreadHelper.ThreadStart()
  InnerException:
it says "Parameter is not valid", but the line in question just says

textBmp = new Bitmap(640, 480); // match window size

Any idea what's going on there?

XNA Distribution Always Failing

25 August 2009 - 09:53 AM

I have spent all day looking up and trying different methods to distribute a .exe file of an XNA game. I made almost the simplest project possible (basically the XNA default game template with one picture added), and then tried the ClickOnce publish, and also tried using Nuclex's XNA installer to detect or install the XNA and .NET frameworks, and tried to have both people who DO have them already and people who don't, and it failed to work on both machines, despite working on mine. I am absolutely out of ideas as to why this isn't working, let alone how to make it work. I'm programming this on Vista, and I've tried using both Visual C# Express 2008 and SharpDevelop. If anyone has any further suggestions, I'm all ears.

Side-by-Side Configuration Error

09 May 2009 - 07:28 AM

I'm aware that this problem has been discussed before, but the solutions to those don't seem to be helping me, so I'll ask here. I am creating a C++ program using Visual Studio, and it runs fine on my computers (one running XP and one running Vista), but when anyone else tries to run it, they get the error message "The application has failed to start because its side-by-side configuration is incorrect. Please see the application event log for more details." No application event log seems to be produced. I'm using Microsoft Visual C++, in Microsoft Visual Studio 2005: Standard Edition. I'm compiling in Release mode, using Multithreaded DLLs. What other information should I be providing?

Trying to pinpoint system specs

01 November 2006 - 06:48 PM

I'm just about to submit my latest game to the Independent Games Festival, and the submission form has a section on hardware requirements. How does one go about figuring this out? It's not a terribly taxing program - a moderate number of non-textured polygons and some bitmap fonts. The game can be downloaded at the URL below. Could anyone give me an estimate as to what I should list as the system reqs? http://professor.fireandrobot.com/zct.zip Thanks in advance.

AI-controlled beings finding ideal targets in a tile-based map

17 June 2006 - 08:22 AM

I am working on a game (Titled Zombie City Tactics) that requires enemy AI more advanced than I'm used to working with. One particular thing is giving me trouble - the enemy units selecting the best target to approach. Take this situation, for example: http://img.photobucket.com/albums/v26/professorscissors/targetfind1.gif The green square representing the moving unit, the red squares representing potential targets, and the gray squares representing walls. Now, a stupid AI would assume the target on the left was better, because it's two squares away, whereas the other target is three squares away. However, a decently intelligent AI would see it like this: http://img.photobucket.com/albums/v26/professorscissors/targetfind2.gif And see that when it comes to actualy path length, the target on the right is much closer. My problem is that I'm trying to find a way to do this *quickly.* My current method should work, but gets very slow in large maps with lots of beings. What it does is this: http://img.photobucket.com/albums/v26/professorscissors/targetfind3.gif The yellow squares are the spaces it is looking at, because they are next to green squares. If it doesn't find any targets there, it marks the yellow squares as 'explored' squares, and iterates again: http://img.photobucket.com/albums/v26/professorscissors/targetfind4.gif Still no targets, so it iterates one more time: http://img.photobucket.com/albums/v26/professorscissors/targetfind5.gif And then it find the target on the right, and selects it. It wouldn't find the left target for another 3 iterations. If no new yellow squares are added in one iteration, it assumes no target and gives up. This works fairly well, but has the downside that it takes HUGE amounts of time in large, open areas. Can anyone here suggest a preferable method? Thanks in advance.

PARTNERS