Exemple #1
0
void NTextInput::Tick(double DT)
{
    if (!DisplayText)
    {
        DisplayText = new NText("cousine",_t(""));
        DisplayText->SetParent(this);
        DisplayText->SetSize(13);
    }
    if (GetGame()->GetInput()->GetMouseKey(0))
    {
        if (Intersects(glm::vec4(GetRealPos().x,GetRealPos().y,GetScale().x,GetScale().y),GetGame()->GetInput()->GetMouse()))
        {
            GetGame()->GetInput()->SetFocus(true);
            DisplayCursor = true;
            Time = 0;
            HasFocus = true;
        } else {
            GetGame()->GetInput()->SetFocus(false);
            HasFocus = false;
        }
    }
    if (HasFocus)
    {
        DisplayText->SetPos(glm::vec3(-GetScale().x/2.f,0,0));
        DisplayText->SetBorder(GetScale().x,GetScale().y);
        Time += DT;
        if (Time>1)
        {
            DisplayCursor = !DisplayCursor;
            Time = 0;
        }
        std::vector<int> StringInput = GetGame()->GetInput()->GetStringInput();
        for (unsigned int i=0;i<StringInput.size();i++)
        {
            RealText.push_back(StringInput[i]);
            if (StringInput[i] == GLFW_KEY_BACKSPACE)
            {
                RealText.erase(RealText.end()-1);
                if (RealText.size()>0)
                {
                    RealText.erase(RealText.end()-1);
                }
            }
        }
    } else {
        DisplayCursor = false;
    }
    if (DisplayCursor)
    {
        DisplayText->SetText(RealText+_t("_"));
    } else {
        DisplayText->SetText(RealText);
    }
    if (!GetGame()->GetInput()->GetFocus())
    {
        DisplayCursor = false;
        HasFocus = false;
    }
}
Exemple #2
0
//Here we do a bunch of ifelseifelseifififelse to make our little quad act like a button.
void NButton::Tick(double DT)
{
    IsPressed = false;
    //Make sure if we have a texture to move it forward in time, just in case it's animated.
    if (Texture)
    {
        Texture->Tick(DT);
    }
    if (ShrinkWrap && DisplayText)
    {
        SetScale(glm::vec3(DisplayText->GetWidth(),GetScale().y,GetScale().z));
    }
    glm::vec2 MP = GetGame()->GetInput()->GetMouse();
    //Check if the mouse intersects with button
    if (Intersects(glm::vec4(GetRealPos().x,GetRealPos().y,GetScale().x,GetScale().y),MP))
    {
        if (GetGame()->GetInput()->GetMouseKey(0))
        {
            if (Texture)
            {
                Texture->Play("pressed");
            }
            IsPressed = true;
        } else {
            if (Texture)
            {
                Texture->Play("active");
            }
        }
        if (IsPressed != PressedMemory)
        {
            IsChanged = true;
            PressedMemory = IsPressed;
        } else {
            IsChanged = false;
        }
    } else {
        PressedMemory = false;
        IsChanged = false;
        if (Texture)
        {
            Texture->Play("idle");
        }
    }
    if (Toggleable)
    {
        if (IsPressed && IsChanged)
        {
            Toggled = !Toggled;
        }
        if (Toggled)
        {
            Texture->Play("pressed");
        }
    }
}
Exemple #3
0
void NEntity::Draw(NCamera* View)
{
    if (GetGame()->GetMap()->GetLevel() != GetGame()->GetMap()->GetLevel(GetRealPos()))
    {
        return;
    }

    GenerateBuffers();
    //Make sure we can draw
    if (Texture == NULL || GetColor().w == 0 || Shader == NULL)
    {
        return;
    }
    glUseProgram(Shader->GetID());
    glActiveTexture(GL_TEXTURE0);
    if (Texture != NULL)
    {
        glBindTexture(GL_TEXTURE_2D,Texture->GetID());
    }
    glUniform1i(TextureLoc,0);
    glUniformMatrix4fv(MatrixLoc,1,GL_FALSE,glm::value_ptr(GetModelMatrix()));
    glUniform4fv(ColorLoc,1,&(GetColor()[0]));
    glEnableVertexAttribArray(Shader->GetVertexAttribute());
    glBindBuffer(GL_ARRAY_BUFFER,Buffers[0]);
    glVertexAttribPointer(Shader->GetVertexAttribute(),2,GL_FLOAT,GL_FALSE,0,NULL);
    glEnableVertexAttribArray(Shader->GetUVAttribute());
    glBindBuffer(GL_ARRAY_BUFFER,Buffers[1]);
    glVertexAttribPointer(Shader->GetUVAttribute(),2,GL_FLOAT,GL_FALSE,0,NULL);
    
    glEnable(GL_BLEND);
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
    glEnable(GL_TEXTURE_2D);
    glDrawArrays(GL_QUADS,0,Verts.size());
    glDisable(GL_TEXTURE_2D);
    glDisable(GL_BLEND);

    glDisableVertexAttribArray(Shader->GetVertexAttribute());
    glDisableVertexAttribArray(Shader->GetUVAttribute());
    glUseProgram(0);
}
Exemple #4
0
void NLight::Draw(NCamera* View)
{
    //If the light isn't on the current level of the map, don't draw it! This is required because 2d lighting!
    if (GetGame()->GetMap()->GetLevel() != GetGame()->GetMap()->GetLevel(GetRealPos()))
    {
        return;
    }
    //Clear out the stencil buffer with 0's so we have a clean slate to work with.
    glClear(GL_STENCIL_BUFFER_BIT);
    glEnable(GL_STENCIL_TEST);
    //Make it so whatever we draw replaces everything it touches in the stencil to 1.
    glStencilFunc(GL_ALWAYS,0x1,0x1);
    glStencilOp(GL_KEEP, GL_KEEP, GL_REPLACE);
    //Draw all our shadow volumes.
    DrawShadow(View);
    //Now we make it so we can only draw on 0's, we also don't want to replace anything in the stencil buffer so we lock it up.
    glStencilFunc(GL_EQUAL,0x0,0x1);
    glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);
    //Finally draw the light into whatever's not shadow.
    DrawLight(View);
    glDisable(GL_STENCIL_TEST);
}
Exemple #5
0
void NLight::GenerateShadowBuffers()
{
    //oh boy why didn't i comment this while i coded it; it's pretty damn complicated. I'll try my best to reinterpret and explain what i was thinking.
    if (!Texture)
    {
        return;
    }
    //Compares it's current position and scale from memory, if it hasn't changed from last generation then don't regenerate it!
    if (PositionMemory == GetRealPos() && ScaleMemory == GetScale())
    {
        return;
    }
    glm::vec3 Pos = GetRealPos();
    Shadows.clear();
    //Figures out what size of a box of map tiles we should loop through. FIXME: Make it more accurately decide which map tiles to loop through.
    float Max = std::max(GetScale().x, GetScale().y);
    //For every map tile in a massive area around the light...
    for (float x = -Max;x<Max;x+=GetGame()->GetMap()->GetTileSize())
    {
        for (float y = -Max;y<Max;y+=GetGame()->GetMap()->GetTileSize())
        {
            //Get the tile object at the current loop position
            NTile* Tile = GetGame()->GetMap()->GetTile(Pos+glm::vec3(x,y,0));
            //If the tile doesn't block light or doesn't exist, skip it!
            if (Tile == NULL || !Tile->IsOpaque())
            {
                continue;
            }
            float TS = GetGame()->GetMap()->GetTileSize()/2.f;
            //Generate points in the four corners of the tile.
            glm::vec3 TPos = GetGame()->GetMap()->TilePos(Pos+glm::vec3(x,y,0));
            glm::vec3 Points[4];
            Points[0] = TPos+glm::vec3(TS,TS,0);
            Points[1] = TPos+glm::vec3(TS,-TS,0);
            Points[2] = TPos+glm::vec3(-TS,-TS,0);
            Points[3] = TPos+glm::vec3(-TS,TS,0);
            std::vector<glm::vec4> Faces;
            //Generate faces (two points = face, each tile has 4 faces)
            NTile* CheckTile = GetGame()->GetMap()->GetTile(Tile->X,Tile->Y-1,Tile->Z);
            if ((CheckTile && !CheckTile->IsOpaque()) || !CheckTile)
            {
                Faces.push_back(glm::vec4(Points[1].x,Points[1].y,Points[2].x,Points[2].y));
            }
            CheckTile = GetGame()->GetMap()->GetTile(Tile->X,Tile->Y+1,Tile->Z);
            if ((CheckTile && !CheckTile->IsOpaque()) || !CheckTile)
            {
                Faces.push_back(glm::vec4(Points[3].x,Points[3].y,Points[0].x,Points[0].y));
            }
            CheckTile = GetGame()->GetMap()->GetTile(Tile->X-1,Tile->Y,Tile->Z);
            if ((CheckTile && !CheckTile->IsOpaque()) || !CheckTile)
            {
                Faces.push_back(glm::vec4(Points[2].x,Points[2].y,Points[3].x,Points[3].y));
            }
            CheckTile = GetGame()->GetMap()->GetTile(Tile->X+1,Tile->Y,Tile->Z);
            if ((CheckTile && !CheckTile->IsOpaque()) || !CheckTile)
            {
                Faces.push_back(glm::vec4(Points[0].x,Points[0].y,Points[1].x,Points[1].y));
            }
            for (unsigned int i=0;i<Faces.size();i++)
            {
                //Remove front faces
                if (!Facing(glm::vec2(Pos.x,Pos.y),Faces[i]))
                {
                    continue;
                }
                //Generate shadow mesh by extruding back faces.
                float Radians = -atan2(Pos.x-Faces[i].x,Pos.y-Faces[i].y)-PI/2.f;
                float BRadians = -atan2(Pos.x-Faces[i].z,Pos.y-Faces[i].w)-PI/2.f;
                Shadows.push_back(glm::vec3(Faces[i].x,Faces[i].y,TPos.z));
                Shadows.push_back(glm::vec3(Faces[i].z,Faces[i].w,TPos.z));
                Shadows.push_back(glm::vec3(Faces[i].z+cos(BRadians)*GetScale().x,Faces[i].w+sin(BRadians)*GetScale().y,TPos.z));
                Shadows.push_back(glm::vec3(Faces[i].x+cos(Radians)*GetScale().x,Faces[i].y+sin(Radians)*GetScale().y,TPos.z));
            }
        }
    }

    //3d lighting time, honestly the 2d doesn't work when there's any openings between floors.
    /*for (float x = -Max;x<Max;x+=GetGame()->GetMap()->GetTileSize())
    {
        for (float y = -Max;y<Max;y+=GetGame()->GetMap()->GetTileSize())
        {
            for (float z = -Max;z<Max;z+=GetGame()->GetMap()->GetTileSize())
            {
                //Get the tile object at the current loop position
                NTile* Tile = GetGame()->GetMap()->GetTile(Pos+glm::vec3(x,y,z));
                //If the tile doesn't block light or doesn't exist, skip it!
                if (Tile == NULL || !Tile->IsOpaque())
                {
                    continue;
                }
                float TS = GetGame()->GetMap()->GetTileSize()/2.f;
                //Generate points in the eight verts of the tile.
                glm::vec3 TPos = GetGame()->GetMap()->TilePos(Pos+glm::vec3(x,y,z));
                glm::vec3 Points[8];
                Points[0] = TPos+glm::vec3(TS,TS,0);
                Points[1] = TPos+glm::vec3(TS,-TS,0);
                Points[2] = TPos+glm::vec3(-TS,-TS,0);
                Points[3] = TPos+glm::vec3(-TS,TS,0);
                Points[4] = TPos+glm::vec3(TS,TS,TS*2.f);
                Points[5] = TPos+glm::vec3(TS,-TS,TS*2.f);
                Points[6] = TPos+glm::vec3(-TS,-TS,TS*2.f);
                Points[7] = TPos+glm::vec3(-TS,TS,TS*2.f);
                std::vector< std::vector<glm::vec3> > Faces;
                //Generate faces (four points = face, each tile has 6 faces)
                NTile* CheckTile = GetGame()->GetMap()->GetTile(Tile->X,Tile->Y-1,Tile->Z);
                if ((CheckTile && !CheckTile->IsOpaque()) || !CheckTile)
                {
                    //Faces.push_back(glm::vec4(Points[1].x,Points[1].y,Points[2].x,Points[2].y));
                    std::vector<glm::vec3> Face;
                    Face.push_back(Points[1]);
                    Face.push_back(Points[2]);
                    Face.push_back(Points[6]);
                    Face.push_back(Points[5]);
                    Faces.push_back(Face);
                }
                CheckTile = GetGame()->GetMap()->GetTile(Tile->X,Tile->Y+1,Tile->Z);
                if ((CheckTile && !CheckTile->IsOpaque()) || !CheckTile)
                {
                    //Faces.push_back(glm::vec4(Points[3].x,Points[3].y,Points[0].x,Points[0].y));
                    std::vector<glm::vec3> Face;
                    Face.push_back(Points[3]);
                    Face.push_back(Points[0]);
                    Face.push_back(Points[4]);
                    Face.push_back(Points[7]);
                    Faces.push_back(Face);
                }
                CheckTile = GetGame()->GetMap()->GetTile(Tile->X-1,Tile->Y,Tile->Z);
                if ((CheckTile && !CheckTile->IsOpaque()) || !CheckTile)
                {
                    //Faces.push_back(glm::vec4(Points[2].x,Points[2].y,Points[3].x,Points[3].y));
                    std::vector<glm::vec3> Face;
                    Face.push_back(Points[2]);
                    Face.push_back(Points[3]);
                    Face.push_back(Points[7]);
                    Face.push_back(Points[6]);
                    Faces.push_back(Face);
                }
                CheckTile = GetGame()->GetMap()->GetTile(Tile->X+1,Tile->Y,Tile->Z);
                if ((CheckTile && !CheckTile->IsOpaque()) || !CheckTile)
                {
                    //Faces.push_back(glm::vec4(Points[0].x,Points[0].y,Points[1].x,Points[1].y));
                    std::vector<glm::vec3> Face;
                    Face.push_back(Points[0]);
                    Face.push_back(Points[1]);
                    Face.push_back(Points[5]);
                    Face.push_back(Points[4]);
                    Faces.push_back(Face);
                }
                CheckTile = GetGame()->GetMap()->GetTile(Tile->X,Tile->Y,Tile->Z-1);
                if ((CheckTile && !CheckTile->IsOpaque()) || !CheckTile)
                {
                    std::vector<glm::vec3> Face;
                    Face.push_back(Points[0]);
                    Face.push_back(Points[1]);
                    Face.push_back(Points[3]);
                    Face.push_back(Points[2]);
                    Faces.push_back(Face);
                }
                CheckTile = GetGame()->GetMap()->GetTile(Tile->X,Tile->Y,Tile->Z+1);
                if ((CheckTile && !CheckTile->IsOpaque()) || !CheckTile)
                {
                    std::vector<glm::vec3> Face;
                    Face.push_back(Points[4]);
                    Face.push_back(Points[5]);
                    Face.push_back(Points[7]);
                    Face.push_back(Points[6]);
                    Faces.push_back(Face);
                }
                for (unsigned int i=0;i<Faces.size();i++)
                {
                    //Remove front faces
                    if (Facing(glm::vec3(Pos.x,Pos.y,Pos.z),Faces[i]))
                    {
                        continue;
                    }
                    //Generate shadow mesh by extruding back faces.
                    //float Radians = -atan2(Pos.x-Faces[i].x,Pos.y-Faces[i].y)-PI/2.f;
                    //float BRadians = -atan2(Pos.x-Faces[i].z,Pos.y-Faces[i].w)-PI/2.f;
                    glm::vec3 ExtrudeA = glm::normalize(Faces[i][0]-Pos);
                    glm::vec3 ExtrudeB = glm::normalize(Faces[i][1]-Pos);
                    Shadows.push_back(Faces[i][0]);
                    Shadows.push_back(Faces[i][1]);
                    Shadows.push_back(Faces[i][0]+ExtrudeA*GetScale()); //these go very far away so shadows are long
                    Shadows.push_back(Faces[i][1]+ExtrudeB*GetScale());

                    glm::vec3 ExtrudeA = glm::normalize(Faces[i][1]-Pos);
                    glm::vec3 ExtrudeB = glm::normalize(Faces[i][2]-Pos);
                    Shadows.push_back(Faces[i][1]);
                    Shadows.push_back(Faces[i][2]);
                    Shadows.push_back(Faces[i][1]+ExtrudeA*GetScale());
                    Shadows.push_back(Faces[i][2]+ExtrudeB*GetScale());

                    ExtrudeA = glm::normalize(Faces[i][2]-Pos);
                    ExtrudeB = glm::normalize(Faces[i][3]-Pos);
                    Shadows.push_back(Faces[i][2]);
                    Shadows.push_back(Faces[i][3]);
                    Shadows.push_back(Faces[i][2]+ExtrudeA*GetScale());
                    Shadows.push_back(Faces[i][3]+ExtrudeB*GetScale());

                    ExtrudeA = glm::normalize(Faces[i][3]-Pos);
                    ExtrudeB = glm::normalize(Faces[i][0]-Pos);
                    Shadows.push_back(Faces[i][3]);
                    Shadows.push_back(Faces[i][0]);
                    Shadows.push_back(Faces[i][3]+ExtrudeA*GetScale());
                    Shadows.push_back(Faces[i][0]+ExtrudeB*GetScale());
                }
            }
        }
    }*/

    glBindBuffer(GL_ARRAY_BUFFER,Buffers[2]);
    glBufferData(GL_ARRAY_BUFFER,Shadows.size()*sizeof(glm::vec3),&Shadows[0],GL_STATIC_DRAW);
    PositionMemory = GetRealPos();
    ScaleMemory = GetScale();
}