Tuesday, August 30, 2016

Getting good at: C++ | #3 - Writing our first program

Before we start...

This blog has like 65 views in a week. O_O



3 - Actually Writing The Program

Alright, so now that you know how a program works, let's write one.

Just make sense out of this:

#include <windows.h>
#include <gl/gl.h>

LRESULT CALLBACK WindowProc(HWND, UINT, WPARAM, LPARAM);
void EnableOpenGL(HWND hwnd, HDC*, HGLRC*);
void DisableOpenGL(HWND, HDC, HGLRC);


int WINAPI WinMain(HINSTANCE hInstance,
                   HINSTANCE hPrevInstance,
                   LPSTR lpCmdLine,
                   int nCmdShow)
{
    WNDCLASSEX wcex;
    HWND hwnd;
    HDC hDC;
    HGLRC hRC;
    MSG msg;
    BOOL bQuit = FALSE;
    float theta = 0.0f;


    wcex.cbSize = sizeof(WNDCLASSEX);
    wcex.style = CS_OWNDC;
    wcex.lpfnWndProc = WindowProc;
    wcex.cbClsExtra = 0;
    wcex.cbWndExtra = 0;
    wcex.hInstance = hInstance;
    wcex.hIcon = LoadIcon(NULL, IDI_APPLICATION);
    wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
    wcex.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
    wcex.lpszMenuName = NULL;
    wcex.lpszClassName = "GLSample";
    wcex.hIconSm = LoadIcon(NULL, IDI_APPLICATION);;


    if (!RegisterClassEx(&wcex))
        return 0;

    hwnd = CreateWindowEx(0,
                          "GLSample",
                          "OpenGL Sample",
                          WS_OVERLAPPEDWINDOW,
                          CW_USEDEFAULT,
                          CW_USEDEFAULT,
                          256,
                          256,
                          NULL,
                          NULL,
                          hInstance,
                          NULL);

    ShowWindow(hwnd, nCmdShow);

    EnableOpenGL(hwnd, &hDC, &hRC);


    while (!bQuit)
    {

        if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
        {

            if (msg.message == WM_QUIT)
            {
                bQuit = TRUE;
            }
            else
            {
                TranslateMessage(&msg);
                DispatchMessage(&msg);
            }
        }
        else
        {
           

            glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
            glClear(GL_COLOR_BUFFER_BIT);

            glPushMatrix();
            glRotatef(theta, 0.0f, 0.0f, 1.0f);

            glBegin(GL_TRIANGLES);

                glColor3f(1.0f, 0.0f, 0.0f);   glVertex2f(0.0f,   1.0f);
                glColor3f(0.0f, 1.0f, 0.0f);   glVertex2f(0.87f,  -0.5f);
                glColor3f(0.0f, 0.0f, 1.0f);   glVertex2f(-0.87f, -0.5f);

            glEnd();

            glPopMatrix();

            SwapBuffers(hDC);

            theta += 1.0f;
            Sleep (1);
        }
    }


    DisableOpenGL(hwnd, hDC, hRC);


    DestroyWindow(hwnd);

    return msg.wParam;
}

LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
    switch (uMsg)
    {
        case WM_CLOSE:
            PostQuitMessage(0);
        break;

        case WM_DESTROY:
            return 0;

        case WM_KEYDOWN:
        {
            switch (wParam)
            {
                case VK_ESCAPE:
                    PostQuitMessage(0);
                break;
            }
        }
        break;

        default:
            return DefWindowProc(hwnd, uMsg, wParam, lParam);
    }

    return 0;
}

void EnableOpenGL(HWND hwnd, HDC* hDC, HGLRC* hRC)
{
    PIXELFORMATDESCRIPTOR pfd;

    int iFormat;


    *hDC = GetDC(hwnd);


    ZeroMemory(&pfd, sizeof(pfd));

    pfd.nSize = sizeof(pfd);
    pfd.nVersion = 1;
    pfd.dwFlags = PFD_DRAW_TO_WINDOW |
                  PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
    pfd.iPixelType = PFD_TYPE_RGBA;
    pfd.cColorBits = 24;
    pfd.cDepthBits = 16;
    pfd.iLayerType = PFD_MAIN_PLANE;

    iFormat = ChoosePixelFormat(*hDC, &pfd);

    SetPixelFormat(*hDC, iFormat, &pfd);


    *hRC = wglCreateContext(*hDC);

    wglMakeCurrent(*hDC, *hRC);
}

void DisableOpenGL (HWND hwnd, HDC hDC, HGLRC hRC)
{
    wglMakeCurrent(NULL, NULL);
    wglDeleteContext(hRC);
    ReleaseDC(hwnd, hDC);
}


Okay fine, we'll start with something easier.

So before you do anything, start with these 2 lines:

#include <iostream>
using namespace std;

If you don't know what these do, then look at the second blog post, because I'm not explaining it again.

Now, let's make our main variable:

int main()
{

}

Now I haven't told you guys this, but you can add more than one variable to a program. Today, we're going to start out by creating a variable within a variable. The difference between standalone variables (int main(), etc...) and nested variables (int memes) is that with nested variables, you don't need to add parameters and a function to it, whereas with standalone variables, you do. So, to create a nested variable, you need to go into your function, and type:

int a;
a = 69;

or

int a = 69;

This means you have created a variable, and set the value of it to 69 (hehe 69). To display this variable, type:

cout << a << endl;

In case you're wondering, endl; ends the line so text can appear below it.

And of course, finish it off with:

return 0;

After you're finished, your program should look something like this:




For the .01% of you who are wondering how I got the command console to output our variable, you have to go to the toolbar just below the top, and click on this icon.

And that's all there is to it. Next post, I'm going to be covering User-defined variables. Until next time...


Getting good at: C++ | #2 - Hello World Program

Just before we start...

Okay, so I posted this like 5 minutes ago and already has like 25 views. wat

2 - Hello World

Okay, did you survive the first post? Good. I'm never doing that again. Anyway, you're probably wondering "oi ow do u make a program if u dont ell me im the sikest bl0ke ul ever met il call up my m8s an were gonna W1N$T0N u i swear on me mum il rek u so hard r00nscape 1v1 after sk00l". The first thing you'll see after opening up Code::Blocks is this:

Select "Create new project", "Console Application" (for now), "Next >", "Next >", then name your project and make a folder for your .cpp files. After that, click "Next >" and "Finish". Now, after you're done, you should see something like this:


This is a simple "Hello World" program that loads automatically. Like, right after you create the project. So this it how it works:

#include <iostream>

#include is not a Twitter hashtag, rather, it's what allows you to refer to other classes and objects (Often in the form of a ".h" file). For example, let's say that you're writing a program that refers to many different classes. To refer to all of them, you would need to write something like this:

#include <iostream>
#include <string>
#include <"stdio.h">
#include <"dankmemes.h>
#include <"urmum.h">


using namespace std;

No, this doesn't stand for Sexually Transmitted Disease. In C++, std
stands for "standard". In most cases, you would need to include a prefix  to let C++ know that you're using the standard library with your programs.  "using namespace std;" means that your program won't look like this:

#include <iostream>

int main()
{
    std::cout << "end it kid" << std::endl;
    return 0;
}

In other words, you don't have to type as much.

int main()
{
}

This is your main variable. Without this, your compiler's gonna have an aneurysm.

Okay, that's not entirely true, but your program won't compile without it. The empty parentheses are called "parameters". Parameters store values which are passed into functions. The empty curly braces are where your function is stored. A function is the "meat and potatoes" of your program. It's here where we're gonna write the command that will print "Hello World" on a screen. If you still don't understand, here's an example:

int main()
{
    cout << "end it" << endl;
    return 0;
}

"cout" is what allows you to print stuff out on a screen. When you use "cout", you must use << and give it something to print. NOTE THAT IT'S CRUCIAL TO INCLUDE SEMI-COLONS. Return 0 tells the compiler that the program has nothing to return, which means that your program ran successfully.

That's really it for now. Until next time...




Thursday, August 25, 2016

Getting good at: C++ | #1 - Installing Code::Blocks

Introduction


Image result for shhh can you hear that the sound of git gud
Hello m8s. If you're reading this blog, then that means you suck @ c++ and you want to git gud. Not to worry, daddys got a really spicy meme for you magnificent scrubs.



1 - Installing Code::Blocks

DISCLAIMER: If you've already installed Code::Blocks prior to reading this post than why are you even here. Go watch thenewboston or something.

This is the hardest part, so read carefully:

Step 1: Go to codeblocks.org

Step 2: Click on "Downloads"

Step 3: Select "Download the binary release"

Step 4: Select "Windows XP / Vista..."

NOTE: You can also install this on Ubuntu/Debian/OpenSUSE/Fedora/RedHat, but it's at least 100 times harder.

Step 5: Where you see "codeblocks-16.01mingw-setup.exe", select "sourceforge.net". (You could use FossHub, but like no one uses FossHub so...)

Step 6 (Note that this is like the hardest part so read carefully): Once it's finished, select "Run"

NOTE: This may vary for other browsers.

Step 7: It should show up with an install wizard. Click next at least 100 times. You don't have to worry about losing plugins because it already comes with them pre-selected.

Step 8: This one's a little different. Click "Install".

Step 9: wait.

Step 10: Code::Blocks will ask you if you would like to run it now. If you want to git gud fast, click yes. If you want to keep reading your Steven Universe fanfiction for like 2 hours, click no.

Step 11: run it.

That's all I'm gonna post today, so make sure to read the next post when it comes out. Until then, stay tuned.










How to git gud (C++ Edition) #1

Introduction


Image result for shhh can you hear that the sound of git gud
Hello m8s. If you're reading this blog, then that means you suck @ c++ and you want to git gud. Not to worry, daddys got a really spicy meme for you magnificent scrubs.

1 - Installing Code::Blocks

DISCLAIMER: If you've already installed Code::Blocks prior to reading this post than why are you even here. Go watch thenewboston or something.

This is the hardest part, so read carefully:

Step 1: Go to codeblocks.org

Step 2: Click on "Downloads"

Step 3: Select "Download the binary release"

Step 4: Select "Windows XP / Vista..."

NOTE: You can also install this on Ubuntu/Debian/OpenSUSE/Fedora/RedHat, but it's at least 100 times harder.

Step 5: Where you see "codeblocks-16.01mingw-setup.exe", select "sourceforge.net". (You could use FossHub, but like no one uses FossHub so...)

Step 6 (Note that this is like the hardest part so read carefully): Once it's finished, select "Run"

NOTE: This may vary for other browsers.

Step 7: It should show up with an install wizard. Click next at least 100 times. You don't have to worry about losing plugins because it already comes with them pre-selected.

Step 8: This one's a little different. Click "Install".

Step 9: wait.

Step 10: Code::Blocks will ask you if you would like to run it now. If you want to git gud fast, click yes. If you want to keep reading your Steven Universe fanfiction for like 2 hours, click no.

Step 11: run it.

That's all I'm gonna post today, so make sure to read the next post when it comes out. Until then, stay tuned.