How to make my own gui using opengl and SDL and C++ or whateva? :)

Started by
14 comments, last by H3g3m0n 12 years, 6 months ago
I've been on this mission for a long time as well. One can't argue with the suggestions to use other GUI libraries to get a handle on different API's. Once I did this, I found the GWEN UI. You can grab the entire codebase, I learned tons sifting through it.

http://code.google.com/p/gwen/
Advertisement
I have to point out. Maybe you should design you hud and menus for your game prior to making gui code. That's if you are making a game. This will allow you to be very precise about the feature set you require.
From there you can design it to be extendable for future generic use in any game you want to make but for now, you'll have the feature set you need for your game.

UI systems can get very complicated and then require alot of support assets (images, video, icons) to make use of their more advanced features. You run the same risk as people sitting down and coding generic game engines instead of the engine they need for their specific game.

A prime example: Will you need Scroll bar functionality? really? will you actually need it?

it's a default part of most UIs you see in applications, but in your game.. will there actually be enough stuff in any menu or text field requiring scrolling?
This site walks through the basics of creating an immediate mode GUI using SDL -- take a look, it might be what you're after!

- Jason Astle-Adams

HI PGSCreativeDirector,

I'm a advocate for developing your own GUI. If you consider the notion that the entire 3D Game-world is a Graphical User Interface, you can apply your techniques developed for GUI event handling, messaging, and process optimization to other game entities. I'm currently developing my 15th iteration of a GUI taking an Entity/Component Approach (Ref 1,2,3) .

I'm developing my 2D/3D GUI system using OIS for input, Bullet for collision detection, OGRE (DX/OpenGL) for rendering. I use a Entity/Component Model in which Widgets are containers that loosely couple input, collision, and visual components. You can combine these the components in different variations to create different types of widgets, and layer Widgets on top one another to create more complex Gadgets (composite Widget).

If theres any advice I can offer, I would say that the input and collision components are the primary mechanism for User-Interaction. The visual component is secondary, a representation controlled by the state of the interaction. So, I'd suggest you solve how to 1) manage input , 2) create collision shapes, 3) render visuals, respectively.
You should start as i did
you have a manager class that has a list of screens and only renders and unpdate the selected screen, create a control base class that has a rectangle(all controls are rectangles) and delegates to any event you want to add, this base class has to have a method update a a method draw, in the method update check if the mouse is on the rectangle and fire the added events here is a sample in c#, if you don't want to use glut for text use glFonts.




using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;


namespace ShadowEngine.VisualControls
{
public delegate void OnClick();
public delegate string Function();
public delegate string FunctionParameter(object objeto);


public abstract class Control
{
protected string accesibleName = "null";
protected OnClick onClick;
protected Rectangle areaRectangle;
protected Color fontColor = Color.White;
protected int zOrder;
protected int displayList;
protected IntPtr textFont;
protected bool discard;

public string Name
{
get { return accesibleName; }
}
}
}


using System;
using Tao.OpenGl;
using System.Drawing;
using ShadowEngine.Sound;

namespace ShadowEngine.VisualControls
{
public class Button : Control, IControl
{
bool isPressed;
string text = "";
string textHoverS, texPressedS, texNormalS;
int textHover, texPressed, texNormal, texCurrent;
int textWidth;
Vector2 textPosition = new Vector2(0, 0);
Vector2 buttonPos;


public Button(Rectangle rectangle, string text, OnClick onClick)
{
buttonPos.X = rectangle.X;
buttonPos.Y = rectangle.Y;
base.zOrder = 0;
this.text = text;
this.areaRectangle = rectangle;
base.onClick = onClick;
texNormalS = "normal.jpg";
texPressedS = "pressed.jpg";
textHoverS = "hover.jpg";
texCurrent = texNormal;
textWidth = Sprite.FontWidth(Glut.GLUT_BITMAP_HELVETICA_18, text);
textPosition = new Vector2(areaRectangle.X + (areaRectangle.Width - textWidth) / 2,
areaRectangle.Y + ((areaRectangle.Height - 15) / 2) + 14);
this.textFont = Glut.GLUT_BITMAP_HELVETICA_18;

}


public Button(Rectangle rectangle, OnClick onClick, string texHover,
string texPressed, string texNormal, IntPtr textFont)
{
buttonPos.X = rectangle.X;
buttonPos.Y = rectangle.Y;
base.zOrder = 0;
this.areaRectangle = rectangle;
this.onClick = onClick;
this.texNormalS = texNormal;
this.texPressedS = texPressed;
this.textHoverS = texHover;
this.textFont = textFont;
this.fontColor = Color.White;
}

public bool Discard()
{
return base.discard;
}

public int ZOrder()
{
return zOrder;
}

public Vector2 GetPos()
{
return buttonPos;
}

public void SetPos(Vector2 vect)
{
buttonPos = vect;
areaRectangle.X = vect.X;
areaRectangle.Y = vect.Y;
textPosition = new Vector2(areaRectangle.X + (areaRectangle.Width - textWidth) / 2,
areaRectangle.Y + ((areaRectangle.Height - 15) / 2) + 14);
}

public Rectangle GetRectangle()
{
return areaRectangle;
}

public string AccesibleName()
{
return accesibleName;
}

public void Create()
{
textHover = ContentManager.GetTextureByName(textHoverS);
texPressed = ContentManager.GetTextureByName(texPressedS);
texNormal = ContentManager.GetTextureByName(texNormalS);
texCurrent = texNormal;
}

public void Update(Cursor cursor)
{
if (areaRectangle.Contains(cursor.Position))
{
texCurrent = textHover;
if (cursor.State == Glut.GLUT_DOWN)
{
isPressed = true;
texCurrent = texPressed;
}
else

if (isPressed)
{
if (cursor.State == Glut.GLUT_UP)
{
isPressed = false;
texCurrent = texNormal;
if (onClick != null)
{
AudioPlayback.PlayOne("click.wav");
base.onClick.Invoke();
}
}
}
}
else
{
isPressed = false;
texCurrent = texNormal;
}
}

public void Draw()
{
Sprite.DrawSprite(areaRectangle, texCurrent);
Sprite.DrawText(textPosition.X, textPosition.Y, text, textFont, fontColor);
}
}
}


Another possibility is to look at embedding webkit. Then you can write a HTML5 UI, similar to how many commercial games use flash. You also get some neat feature like vector graphics, canvas2D and maybe even video (wouldn't it be great if games allowed you to pause and rewind videos). Since you have a full HTML5 renderer you can get more of game themed UI. It might add a bit of overhead though.

I did something like that with QT's QWebView. Unfortunately the webkit build in QT is currently missing some graphical features like shadows, blur and so on.

Otherwise QT itself can be overlayed over a OpenGL context (also it's possible for the QT widgets to be rendered in a 2D scene). There's some official demo code for that:
http://labs.qt.nokia.com/2008/06/27/accelerate-your-widgets-with-opengl/
http://labs.qt.nokia.com/2008/12/02/widgets-enter-the-third-dimension-wolfenqt/
http://labs.qt.nokia.com/2006/10/27/gloverlay/

Only problem is that QT by default won't exactly look very gameish, but it's great for editing tools or technical stuff.

There's Awesomium (a webkkit binding lib aimed at games, used in Eve online) which has sadly gone commercial (although if you look around you can find an older GPL'd fork) and there's also Berkelium which uses Chromium rather than webket direct, unfortunately it's a massive pain to build/develop since you need to compile all of Chromium rather than just using libwebkit.

This topic is closed to new replies.

Advertisement