Since OpenGL uses the strange system of 1s and 0s to represent location on the screen, I'm trying to figure out how to switch it to standard x, y coordinates like those that are used on a graph. And so far, my searches on the rest of the internet have turned out nothing. So, to clarify:

How do I make it so that the bottom left corner is the starting place of 0,0 instead of the center of the screen, which is where OpenGL Defaults it too?

Here is my code for reference:

#include <iostream>
#include <SDL2/SDL.h>
#include <string>
#include <GL/glew.h>
#include <GL/GLU.h>

std::string projectName = "Testing The System";

SDL_Window *mainWindow;
SDL_GLContext mainContext;

bool init(int height, int width, std::string programName); //Creates Window and prepares program.
bool SetOpenGLAttributes();
void CheckSDLError(int line); //Finds errors in SDL Initialization
bool RunGame(); //Mainly used for event handling
void Cleanup(); //Shuts down windows, etc.
void Background();

int x, y;

bool init(int height, int width, std::string programName)
{
    //Checks SDL Loading.  If it fails, cancels program.
    if (SDL_Init(SDL_INIT_VIDEO) < 0)
    {
        std::cout << "Critical Error #001: Failed To Initialize SDL.";
        return false;
    }

    mainWindow = SDL_CreateWindow(programName.c_str(), SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, width, height, SDL_WINDOW_OPENGL);

    if (!mainWindow)
    {
        std::cout << "Critical Error #002: Failed To Properly Initialize Window" << std::endl;
        CheckSDLError(__LINE__);
        return false;
    }

    mainContext = SDL_GL_CreateContext(mainWindow);

    SetOpenGLAttributes();

    SDL_GL_SetSwapInterval(1);

    glEnable(GL_TEXTURE_2D);

    return true;
}

bool SetOpenGLAttributes()
{
    SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);

    SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
    SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 2);

    SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);

    return true;
}

void DrawPixel(int x, int y, int r, int g, int b)
{
    x = x / 999;
    y = y / 499;

    glBegin(GL_POINTS);
    glColor3f(r, g, b);
    glVertex2f(x, y);
    glVertex2f(1.0f, 1.0f);
    glEnd();
}

void Background()
{
    DrawPixel(900, 400, 1, 1, 1);
}

int main(int argc, char *argv[])
{
    bool loop = true;

    if (!init(500, 1000, "Overwatch by Nerdout"))
    {
        std::cout << "Critical Error #003: Failed to Initialize in int main()" << std::endl;
        return false;
    }

    while (loop)
    {
        glClearColor(0.0, 0.0, 0.0, 1.0);
        glClear(GL_COLOR_BUFFER_BIT);

        Background();

        SDL_GL_SwapWindow(mainWindow);

        if (RunGame() == false)
        {
            break;
        }
    }

    Cleanup();

    return 0;
}

bool RunGame()
{
    bool loop = true;

    button:button button1(61, 0, 61, 0);

    while (loop)
    {

        SDL_Event event;
        while (SDL_PollEvent(&event))
        {
            if (event.type == SDL_QUIT)
            {
                loop = false;
                return loop;
            }

            if (event.type == SDL_KEYDOWN)
            {
                switch (event.key.keysym.sym)
                {
                case SDLK_ESCAPE:
                    loop = false;
                    return loop;
                    break;
                }
            }
            if (event.type == SDL_MOUSEMOTION)
            {
                x = event.motion.x;
                y = event.motion.y;
                std::cout << "(" << x << "," << y << ")" << std::endl;

            }
            if (event.type == SDL_MOUSEBUTTONDOWN)
            {
                if (event.button.button == SDL_BUTTON_LEFT)
                {

                }

                if (event.button.button == SDL_BUTTON_RIGHT)
                {

                }
            }

        }
    }
}

void Cleanup()
{
    // Delete our OpengL context
    SDL_GL_DeleteContext(mainContext);

    // Destroy our window
    SDL_DestroyWindow(mainWindow);

    // Shutdown SDL 2
    SDL_Quit();
}

void CheckSDLError(int line = -1)
{
    std::string error = SDL_GetError();

    if (error != "")
    {
        std::cout << "SLD Error : " << error << std::endl;

        if (line != -1)
            std::cout << "\nLine : " << line << std::endl;

        SDL_ClearError();
    }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.