Programming a Game Interface

Member
Posts: 78
Joined: 2002.06
Post: #16
jamiep Wrote:Will, so in this image:
...
That rounded rectangle in the middle is a Cocoa window, and the
buttons are dragged around in IB?
(These buttons were made with your NSButton subclass? Does that
involve OpenGL or no?).
Yes, the rounded rectangle is just a Cocoa window, you can see it in the MainMenu.nib (though the images might not load for you), have a look. The buttons can be dragged resized connected up etc, they are just standard buttons. There is no OpenGL anywhere in my interface, apart for the background window.
Quote this message in a reply
Sage
Posts: 1,066
Joined: 2004.07
Post: #17
ferum Wrote:I've read the man pages on all of these
i'm sorry to be off topic but what are these "man pages" of which people speak of?
If you go into the terminal, the man pages are manual pages of sorts. Try typing 'man glVertex' into the terminal.
Quote this message in a reply
Member
Posts: 89
Joined: 2005.06
Post: #18
Xcode also does a nice job of bringing man pages within easier reach. You can access them through the help menu. I my self have remapped M to bring up the man page prompt.
Quote this message in a reply
Member
Posts: 89
Joined: 2005.06
Post: #19
Ok so I've run into some problems, and I was wondering if I could get some help. I am building my elements in a 3x3 grid, i.e.
top-left | top-mid | top-right.
left-mid | mid-mid | right-mid.
bot-left | bot-mid | bot-right.

the only problem is that my *-mid, when I stretch the texture to span the width of the object, I get some blurry stuff on the ends. I can't seem to figure out where it is coming from, I'll post code and a picture:

Picture:
[Image: texture_wtf.png]

Code:
Code:
void iButton::draw(void) {
    
    glPushMatrix();
    
    glEnable(GL_TEXTURE_2D);
    glEnable(GL_BLEND);
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
    
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);

    texture->bind();
    
    float tex = (float)clicked/2;
    
    glBegin(GL_QUADS);
    
    // Top Middle
    glTexCoord2f(0.50+tex, 0.00);    glVertex2f(x+w-tw, y);
    glTexCoord2f(0.25+tex, 0.00);    glVertex2f(x+tw, y);
    glTexCoord2f(0.25+tex, 1.00);    glVertex2f(x+tw, y+th);
    glTexCoord2f(0.50+tex, 1.00);    glVertex2f(x+w-tw, y+th);
    
    // Top Left
    glTexCoord2f(0.25+tex, 0.00);    glVertex2f(x+tw, y);
    glTexCoord2f(0.00+tex, 0.00);    glVertex2f(x, y);
    glTexCoord2f(0.00+tex, 1.00);    glVertex2f(x, y+th);
    glTexCoord2f(0.25+tex, 1.00);    glVertex2f(x+tw, y+th);
    
    // Middle Left
    glTexCoord2f(0.50+tex, 0.00);    glVertex2f(x+tw, y+th);
    glTexCoord2f(0.25+tex, 0.00);    glVertex2f(x, y+th);
    glTexCoord2f(0.25+tex, 1.00);    glVertex2f(x, y+th+(h-th*2));
    glTexCoord2f(0.50+tex, 1.00);    glVertex2f(x+tw, y+th+(h-th*2));
    
    // Bottom Left
    glTexCoord2f(0.25+tex, 1.00);    glVertex2f(x+tw, y+h-th);
    glTexCoord2f(0.00+tex, 1.00);    glVertex2f(x, y+h-th);
    glTexCoord2f(0.00+tex, 0.00);    glVertex2f(x, y+h);
    glTexCoord2f(0.25+tex, 0.00);    glVertex2f(x+tw, y+h);

    // Middle Middle
    glTexCoord2f(0.50+tex, 0.00);    glVertex2f(x+w-tw, y+th);
    glTexCoord2f(0.25+tex, 0.00);    glVertex2f(x+tw, y+th);
    glTexCoord2f(0.25+tex, 1.00);    glVertex2f(x+tw, y+th+(h-th*2));
    glTexCoord2f(0.50+tex, 1.00);    glVertex2f(x+w-tw, y+th+(h-th*2));
    
    // Top Right
    glTexCoord2f(0.00+tex, 0.00);    glVertex2f(x+w, y);
    glTexCoord2f(0.25+tex, 0.00);    glVertex2f(x+w-tw, y);
    glTexCoord2f(0.25+tex, 1.00);    glVertex2f(x+w-tw, y+th);
    glTexCoord2f(0.00+tex, 1.00);    glVertex2f(x+w, y+th);
    
    // Middle Right
    glTexCoord2f(0.50+tex, 0.00);    glVertex2f(x+w, y+th);
    glTexCoord2f(0.25+tex, 0.00);    glVertex2f(x+w-tw, y+th);
    glTexCoord2f(0.25+tex, 1.00);    glVertex2f(x+w-tw, y+th+(h-th*2));
    glTexCoord2f(0.50+tex, 1.00);    glVertex2f(x+w, y+th+(h-th*2));
    
    // Bottom Right
    glTexCoord2f(0.00+tex, 1.00);    glVertex2f(x+w, y+h-th);
    glTexCoord2f(0.25+tex, 1.00);    glVertex2f(x+w-tw, y+h-th);
    glTexCoord2f(0.25+tex, 0.00);    glVertex2f(x+w-tw, y+h);
    glTexCoord2f(0.00+tex, 0.00);    glVertex2f(x+w, y+h);
    
    // Bottom Middle
    glTexCoord2f(0.50+tex, 0.00);    glVertex2f(x+w-tw, y+h-th);
    glTexCoord2f(0.25+tex, 0.00);    glVertex2f(x+tw, y+h-th);
    glTexCoord2f(0.25+tex, 1.00);    glVertex2f(x+tw, y+h);
    glTexCoord2f(0.50+tex, 1.00);    glVertex2f(x+w-tw, y+h);
    
    glEnd();
    glPopMatrix();
    
    glDisable(GL_TEXTURE_2D);
    
    // This is sort of a hack, its not just drawn once so its ok.
    w = font->drawString(x+size/3, y+(size/2), value)+size/2;
}

My other problem is working with my slider object. I can't get it to slide correctly, my architecture must be messed up, here is the code:

Header:
Code:
#pragma once
#include "iControl.h"

class iSlider : public iControl {
public:
    iSlider(int, int, int, int, int, int);
    void draw(void);
    bool events(SDL_Event*, int, int);
    
protected:
    int value;
    int min;
    int max;
    int segs;
    int move;
    int position;
    int sliderSize;
    int fontSize;
    int tw;
    float incriment;
    int cx;
    bool clicked;
    Texture* texture;
    Font* font;
};

Body:
Code:
#include "iSlider.h"
#include "Game.h"
iSlider::iSlider(int x, int y, int w, int min, int max, int segs) {
    this->x = x;
    this->y = y;
    this->w = w;
    this->max = max;
    this->min = min;
    this->value = min;
    this->segs = segs;
    clicked = false;
    
    texture = TEXTURES.load("iGUI/iSlider.png");
    font = FONTS.load("iGUI/ArialBlack", 12);
    
    this->h = texture->getHeight()/2;
    this->tw = texture->getWidth()/2;
    this->incriment = (float)(w+tw)/(float)max-min;
    this->sliderSize = (float)(w+(tw))/(float)segs;
    printf("incriment: %lf\n", incriment);
    this->position = 0;
}

void iSlider::draw(void) {
    glEnable(GL_TEXTURE_2D);
    texture->bind();
    
    glBegin(GL_QUADS);
    // Bottom Left
    glTexCoord2f(0.50, 0.00);    glVertex2f(x+tw, y);
    glTexCoord2f(0.00, 0.00);    glVertex2f(x, y);
    glTexCoord2f(0.00, 0.50);    glVertex2f(x, y+h);
    glTexCoord2f(0.50, 0.50);    glVertex2f(x+tw, y+h);
    
    // Bottom Middle
    glTexCoord2f(1.00, 0.00);    glVertex2f(x+w-tw, y);
    glTexCoord2f(0.50, 0.00);    glVertex2f(x+tw, y);
    glTexCoord2f(0.50, 0.50);    glVertex2f(x+tw, y+h);
    glTexCoord2f(1.00, 0.50);    glVertex2f(x+w-tw, y+h);
    
    // Bottom Right
    glTexCoord2f(0.00, 0.00);    glVertex2f(x+w, y);
    glTexCoord2f(0.50, 0.00);    glVertex2f(x+w-tw, y);
    glTexCoord2f(0.50, 0.50);    glVertex2f(x+w-tw, y+h);
    glTexCoord2f(0.00, 0.50);    glVertex2f(x+w, y+h);
    
    value = ((w/position)*incriment) - min;

    value = value > max ? max : value;
    value = value < min ? min : value;
    
    // Top Left
    glTexCoord2f(0.25, 0.50);    glVertex2f(x+position+(tw/2), y);
    glTexCoord2f(0.00, 0.50);    glVertex2f(x+position, y);
    glTexCoord2f(0.00, 1.00);    glVertex2f(x+position, y+h);
    glTexCoord2f(0.25, 1.00);    glVertex2f(x+position+(tw/2), y+h);
    
    // Top Middle
    glTexCoord2f(1.00, 0.50);    glVertex2f(x+position+(tw/2)+sliderSize, y);
    glTexCoord2f(0.50, 0.50);    glVertex2f(x+position+(tw/2), y);
    glTexCoord2f(0.50, 1.00);    glVertex2f(x+position+(tw/2), y+h);
    glTexCoord2f(1.00, 1.00);    glVertex2f(x+position+(tw/2)+sliderSize, y+h);
    
    // Top Right
    glTexCoord2f(0.00, 0.50);    glVertex2f(x+position+sliderSize+tw, y);
    glTexCoord2f(0.25, 0.50);    glVertex2f(x+position+sliderSize+(tw/2), y);
    glTexCoord2f(0.25, 1.00);    glVertex2f(x+position+sliderSize+(tw/2), y+h);
    glTexCoord2f(0.00, 1.00);    glVertex2f(x+position+sliderSize+tw, y+h);
    
    glEnd();
    fontSize = font->drawString(x+position+(fontSize/2), y, "%d", value);
}

bool iSlider::events(SDL_Event* event, int posX, int posY) {
    if ((posX > x+position && posX < (x+position+sliderSize+tw)) && (posY > y && posY < y+h) || clicked) {
        if (clicked) {
            position = (posX-x)-(cx-x);
            position = position < 0 ? 0 : position;
            position = position > w-tw-sliderSize ? w-tw-sliderSize : position;
        }
        printf("position: %lf\n", position);
        switch(event->type) {
            case SDL_MOUSEBUTTONDOWN:
                clicked = true;
                cx = posX;
                return true;
            case SDL_MOUSEBUTTONUP:
                //if(clicked) action();
                clicked = false;
                return true;
            default:
                return false;
        }
    } else if ((posX > x && posX < x+w) && (posY > y && posY < y+h) || clicked) {
        switch(event->type) {
            case SDL_MOUSEBUTTONDOWN:
                clicked = true;
                return true;
            case SDL_MOUSEBUTTONUP:
                //if(clicked) action();
                clicked = false;
                return true;
            default:
                return false;
        }
    }
    return false;
}

TomorrowPlusX: I know that my textures look awfully similar to yours Wink I'll be changing that once I get the framework down pat.

Well since everyone else is doing it:
Twilight Coders
CIMyAdmin
Whatever
Quote this message in a reply
Member
Posts: 148
Joined: 2003.03
Post: #20
This is how I implemented sliding in my slider widget:

Code:
// uber-pseudo-code
void OnMouseClickClick/OnMouseDrag(Vector2 VMousePoint)
{
    // VMousePoint is in world coordinates
    SlideTo(VMousePoint);
}

void SlideTo(Vector2 VWorldPoint)
{
    // ... Snap VWorldPoint to 'tick mark values' if enabled/necessary
    SetValue(GetValueOfWorldPoint(VWorldPoint));
}

float GetValueOfWorldPoint(Vector2 VWorldPoint)
{
    Vector2f VLocalPoint = VWorldPoint-m_VBoundsMin; // convert to local space
    
    float fWidgetSize;
    fWidgetSize = m_VBoundsMax.X()-m_VBoundsMin.X(); // horizontal slider, use Y() if vertical
    
    float fLocalPointPercentage; // probably a better name for this variable
    fLocalPointPercentage = VLocalPoint.X()/fWidgetSize; // calculate horizontal percentage/ratio, use Y() if vertical slider
    
    // scale and clamp (MinValue not yet supported)
    float fNewValue = fLocalPointPercentage*m_fMaxValue;
    if(fNewValue<0.0f)
    fNewValue = 0.0f;
    else if(fNewValue>m_fMaxValue)
    fNewValue = m_fMaxValue;
    
    return(fNewValue);
}

void SetValue(float fValue)
{
    m_fValue = fValue;
    if(PerformAction()) // allows engine/scripts to cancel change
        UpdateSliderButton();
}

void UpdateSliderButton()
{
    float fWidgetSize;
    fWidgetSize = m_fBoundsWidthScreenSpace; // use Height if vertical
    
    Vector2f VSliderButtonPoint;
    VSliderButtonPoint = Vector2f(m_VBoundsMin.X()+((m_fValue/m_fMaxValue)*fWidgetSize), m_VBoundsCenter.Y()); // adjust as necessary if vertical
    
    m_pkSliderButton->SetTranslate(VSliderButtonPoint);
}
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  Upcoming Multiplatform Game Programming Book for Beginners JazonYamamoto 3 1,262 Apr 14, 2014 05:55 PM
Last Post: JustinFic
  Game engines with object programming JonnyThunder 3 8,152 Aug 16, 2010 10:42 AM
Last Post: SethWillits
  free mac game programming book jaked 4 6,714 Jun 8, 2009 12:48 AM
Last Post: SethWillits
  Game Programming Tutorial markvw 0 4,262 Jan 27, 2009 05:10 AM
Last Post: markvw
  Game Programming for iPhone? Help with Cocoa and MVC. holografix 7 6,589 Jan 22, 2009 08:17 AM
Last Post: AnotherJake