Jump to content

  • Log In with Google      Sign In   
  • Create Account

FREE SOFTWARE GIVEAWAY

We have 4 x Pro Licences (valued at $59 each) for 2d modular animation software Spriter to give away in this Thursday's GDNet Direct email newsletter.


Read more in this forum topic or make sure you're signed up (from the right-hand sidebar on the homepage) and read Thursday's newsletter to get in the running!


lmsmi1

Member Since 11 Jan 2013
Offline Last Active Jun 30 2013 07:57 AM

Topics I've Started

[C++] Clearing Buffer Once

25 June 2013 - 12:39 PM

So I've got this program to run successfully, but am getting a problem: when I hit the return key, it prints a musical note onto the screen. It does do what I want it to do: make a new line, but it prints the musical note at the beginning of the buffer. How do I get rid of this? Where do I place buf.clear() to get the note character to go away? Here's the full code:

 

#include <SFML/Graphics.hpp>
#include <SFML/System.hpp>
#include <SFML/Window.hpp>
#include <vector>

int main() {
    /* Set Variables */
    sf::Color bgColor;
    sf::Font mainFont;
    sf::String pre = "System!>";
    sf::Text prefix;
    sf::String buf;
    sf::Text buffer;
    sf::String lastStr;
    sf::Text lastLine;
    std::vector<sf::String> lineVector;
    int lineCounter = 1;
    int currentVectorPosition = 0;
    int currentPrefixY = 0;
    int i = 0;
    int x = 0;

    bgColor.r = 0;
    bgColor.g = 0;
    bgColor.b = 203;

    sf::RenderWindow rWnd(sf::VideoMode(800, 600), "SFML Window");
    rWnd.setSize(sf::Vector2u(800, 600));
    mainFont.loadFromFile("dos.ttf");

    prefix.setCharacterSize(18);
    prefix.setColor(sf::Color(255, 255, 255));
    prefix.setString(pre);
    prefix.setFont(mainFont);
    prefix.setPosition(0, currentPrefixY);
    prefix.setStyle(sf::Text::Regular);

    buffer.setCharacterSize(18);
    buffer.setColor(sf::Color(255, 255, 255));
    buffer.setFont(mainFont);
    buffer.setPosition(prefix.getPosition().x + prefix.getGlobalBounds().width + 1, 0);
    buffer.setString(buf);
    buffer.setStyle(sf::Text::Regular);

    lastLine.setCharacterSize(18);
    lastLine.setColor(sf::Color(255, 255, 255));
    lastLine.setFont(mainFont);
    lastLine.setPosition(0, 0);
    lastLine.setString(lastStr);
    lastLine.setStyle(sf::Text::Regular);

    while(rWnd.isOpen()) {
        rWnd.clear(bgColor);

        sf::Event event;
        while(rWnd.pollEvent(event)) {
            if(event.type == sf::Event::Closed) {
                rWnd.close();
            }
            if(event.type == sf::Event::KeyPressed) {
                if(event.key.code == sf::Keyboard::Escape) {
                    rWnd.close();
                }
                if(event.key.code == sf::Keyboard::Return) {
                    lineVector.push_back(buf);
                    buf.clear();
                    lineCounter += 1;
                }
            }
            if(event.type == sf::Event::TextEntered) {
                if(event.text.unicode == '\b') {
                    if(!buf.isEmpty()) {
                        buf.erase(buf.getSize() - 1, 1);
                    }
                } else {
                    buf.insert(buf.getSize(), event.text.unicode);
                }
            }
        }
        /* Somewhere between here */
        if(lineCounter == 1) {
            prefix.setPosition(0, 0);
            buffer.setPosition(prefix.getPosition().x + prefix.getGlobalBounds().width + 1, 0);
        } else {
            x = 0;
            i = 1;
            currentVectorPosition = 0;
            currentPrefixY = 0;
            while(i < lineCounter) {
                prefix.setPosition(0, currentPrefixY);
                lastLine.setPosition(prefix.getGlobalBounds().width + 1, prefix.getPosition().y);
                x++;
                lastLine.setString(lineVector[currentVectorPosition]);
                rWnd.draw(prefix);
                rWnd.draw(lastLine);
                currentPrefixY = prefix.getPosition().y + prefix.getGlobalBounds().height;
                i += 1;
                currentVectorPosition += 1;
            }
            prefix.setPosition(0, currentPrefixY);
            buffer.setPosition(prefix.getPosition().x + prefix.getGlobalBounds().width + 1, prefix.getPosition().y/* + prefix.getGlobalBounds().height*/);
        }
        buffer.setString(buf);
        rWnd.draw(prefix);
        rWnd.draw(buffer);
        rWnd.display();
    }
}

[C++ & SDL] Some Questions

20 June 2013 - 09:27 PM

I've got a few questions about using SDL_ttf to render text:

 

1. How can I render the text to a buffer or array?

2. How do I get the length of a single character rendered from a TTF document?

3. If it's possible to do 1 and 2, then how exactly do I render a new line ('\n')?

 

Here's some code I need to modify. Feel free to modify it and post what you can here.

#include "include/SDL/SDL.h"
#include "include/SDL/SDL_ttf.h"

int currentX = 0;
int currentY = 0;
SDL_Surface* screen;
SDL_Surface* fontSurface;
SDL_Color fColor;
SDL_Rect fontRect;

SDL_Event event;

TTF_Font* font;

//Initialize the font, set to white
void fontInit(){
        TTF_Init();
        font = TTF_OpenFont("dos.ttf", 12);
        fColor.r = 0; // 255
        fColor.g = 204; // 255
        fColor.b = 0; //255
}

//Print the designated string at the specified coordinates
void printF(char *c, int x, int y){
        fontSurface = TTF_RenderText_Solid(font, c, fColor);
        fontRect.x = x;
        fontRect.y = y;
        SDL_BlitSurface(fontSurface, NULL, screen, &fontRect);
        SDL_Flip(screen);
}

int main(int argc, char** argv)
{
    // Initialize the SDL library with the Video subsystem
    SDL_Init(SDL_INIT_VIDEO | SDL_INIT_NOPARACHUTE);

    //Create the screen
    screen = SDL_SetVideoMode(320, 480, 0, SDL_SWSURFACE);

    //Initialize fonts
    fontInit();

    //Print to center of screen
    // printF("Hello World", screen->w/2 - 11*3, screen->h/2);
    printF("Hello world!", currentX, currentY);

    do {
        // Process the events
        while (SDL_PollEvent(&Event)) {
            switch (Event.type) {

                case SDL_KEYDOWN:
                    switch (Event.key.keysym.sym) {
                    // Escape forces us to quit the app
                        case SDLK_ESCAPE:
                            event.type = SDL_QUIT;
                        break;

                        default:
                        break;
                    }
                break;

            default:
            break;
        }
    }
    SDL_Delay(10);
    } while (Event.type != SDL_QUIT);

    // Cleanup
    SDL_Quit();

    return 0;
}

Thanks in advance.

 

~lmsmi1


Simulating a console program?

16 June 2013 - 07:25 PM

Apparently standard C++ doesn't offer fullscreen consoles in Win7, so I've come to OpenGL with high hopes. I figure there's some way to make a virtual console in C++ with OpenGL somehow, so let's get to what I want to make:

 

1. I don't want to make an OS, I just want a fullscreen (no window or borders, just a black BG with text on it) application.

2. I want it to look like DOS (refer to 1).

3. I don't want to make a CLI, I want to make a small console program that takes up the whole screen.

 

Does anyone know how to achieve my goal? Do I even need to use OpenGL?


[OpenGL & C++] Show OBJ and Apply Texture

15 January 2013 - 06:35 PM

Hallo again everyone. As you may know, I'm developing an FPS in C++ with GLUT. I've got GLUT setup with Code::Blocks 12.11, and need some help with some things:

 

1.) Reading & Displaying an OBJ file

2.) Applying a texture to the OBJ in the window

 

So I've got no experience with OpenGL except using/modifying the sample code given by Code::Blocks. Does anyone know how to load an OBJ and then apply a texture?


First-Person Shooter [Code::Blocks with MinGW]

11 January 2013 - 05:40 PM

Well, hi there :). I was directed here by another person on a website, and am confident that GD's community will help me tremendously.

 

I am currently starting a very big project called "R3C0N". It is an FPS gme plan that's game engine will be coded in C++. I know a moderate level of C++, including the usual i/o with consoles, and  little Win32 API, however I have absolutely no knowledge of libs such as GDK+, OpenGL, DirectX, etc. When I came up with the idea of an FPS game coded in C++, I planned it out very roughly, then smoothed it out as I researched what was possible with C++. Here is the beta plan:

 

Game Engine - Reads the files neccessary to play the game, displays graphics, etc.

Game Folders - Contain the neccessary files (.wepn, .wav, .obj, .texture, etc.)

 

I have a complete plan if you would like me to post it, but since this is my first post here, I thought I would just ask the bare minimum, as I don't know what the community is like.

 

My main question is: what library is good for FPS games in C++? I'm using MinGW with Code::Blocks, so anything that's compatible with MinGW works for me. I am planning on having the player to be able to scope zoom, sight zoom, you know things like Call of Duty Modern Warfare 3 players can do.

 

Thanks in advance for any help at all. I look forward to meeting the community.

 

~lmsmi1


PARTNERS