-2

i'm sorry if the question layout is weird or something this is my first time asking a question. I have started learning c++ a week ago and now that i know how to create a window with GLFW I would like to clean up my code and make it nicer. I tried to put the "create window" stuff inside a .cpp file named "window.cpp" and then import it to the main file. but when i take out the GLFWCreateWindow function it doesn't recognize the window name "w_gill" in the swapbuffer, windowshouldclose and destroywindow functions. Can anyone please help me?

Here is the Main.cpp file:

#include <iostream>
#include <Windows.h>
#include "window.cpp"

int main(){
    do{
        createWindow();
        glfwSwapBuffers(w_gill);
        glfwPollEvents();
    } while (!glfwWindowShouldClose(w_gill));

    glfwDestroyWindow(w_gill);
    glfwTerminate();
    return 0;
}

And the Window.cpp file:

#include <GL\glew.h>
#include <GLFW\glfw3.h>

int windowWidth = 1920 / 2;
int windowHeight = 1080 / 2;

int createWindow(){
    if (!glfwInit())
    {
        glfwTerminate();
        return -1;
    }
    glfwWindowHint(GLFW_SAMPLES, 4);
    glfwWindowHint(GLFW_VERSION_MAJOR, 3);
    glfwWindowHint(GLFW_VERSION_MINOR, 4);
    glfwWindowHint(GLFW_RESIZABLE, GL_TRUE);

    GLFWwindow* w_gill;
    w_gill = glfwCreateWindow(windowWidth, windowHeight, "Gillow", NULL, NULL);

    if (!w_gill)
    {
        glfwTerminate();
        return -1;
    }
    glfwMakeContextCurrent(w_gill);
    glfwSetInputMode(w_gill, GLFW_STICKY_KEYS, GL_TRUE);
    }
Luka Laxx
  • 11
  • 2
  • 1
    You shouldn't include translation units. Compile them separately and link them together. – πάντα ῥεῖ Jul 08 '15 at 11:02
  • @πάνταῥεῖ how do i link them together? – Luka Laxx Jul 08 '15 at 11:05
  • 1
    This has nothing to do with the code being in separate files - `w_gill` is a local variable in the function. You can return its value and use it in `main`. – molbdnilo Jul 08 '15 at 11:05
  • 2
    If you started learning C++ a week ago, you may find GLFW and 3D graphics being slightly too ambitious a starting point if you're not very familiar with them already. I would recommend that you pick up [a book](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) and start at the beginning. – molbdnilo Jul 08 '15 at 11:13

2 Answers2

1

Nevermind I got it, I had to put

GLFWwindow* w_gill;
int createWindow();

inside of the main.cpp file so they are linked. Thanks for responding anyway.

Luka Laxx
  • 11
  • 2
0

Your problem has to do with the "scope" or lifetime that a variable has in C. The scope of w_gill is local to its containing function, and as such, there is no visibility to that variable outside of the function. In addition to moving the createWindow() function to its own .CPP file, you must have made some other changes, as even if it was still in main.cpp you would still have the same issue.

David LaPorte
  • 231
  • 1
  • 4