#include <SFML/Graphics.hpp>
#include <iostream>
#include <ctime>
#include <cstdio>
#include <cstring>
#include <cmath>
float xpos = 0, ypos = 0, zpos = 0, xrot = 0, yrot = 0, angle = 0.0;
void camera()
{
glRotatef(xrot, 1.0, 0.0, 0.0);
glRotatef(yrot, 0.0, 1.0, 0.0);
glTranslated(-xpos, -ypos, -zpos);
}
int main()
{
unsigned int VideoModesCount = sf::VideoMode::GetModesCount();
for (unsigned int i = 0; i < VideoModesCount; ++i)
{
sf::VideoMode Mode = sf::VideoMode::GetMode(i);
// Mode is a valid video mode
}
// Create the main window
sf::RenderWindow App(sf::VideoMode::GetMode(0), "Torem Online 0.0.1", sf::Style::Fullscreen);
App.PreserveOpenGLStates(true);
// Load an OpenGL mipmap texture.
GLuint Texture = 0;
{
sf::Image Image;
if(!Image.LoadFromFile("datas/opengl/texture.jpg"))
return EXIT_FAILURE;
glGenTextures(1, &Texture);
glBindTexture(GL_TEXTURE_2D, Texture);
gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGBA, Image.GetWidth(), Image.GetHeight(), GL_RGBA, GL_UNSIGNED_BYTE, Image.GetPixelsPtr());
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
}
// Create a clock for measuring time elapsed
sf::Clock Clock;
// Enable Z-buffer read and write
glEnable(GL_DEPTH_TEST);
glDepthMask(GL_TRUE);
glClearDepth(1.f);
// Setup a perspective projection
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(90.f, 1.f, 1.f, 500.f);
// Bind our mipmap texture
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, Texture);
glColor4f(1.f, 1.f, 1.f, 1.f);
sf::Image ScreenShotImage;
time_t rawtime;
struct tm * timeinfo;
char buffer[40];
// Start game loop
while (App.IsOpened())
{
// Process events
sf::Event Event;
while (App.GetEvent(Event))
{
// Close window : exit
if (Event.Type == sf::Event::Closed)
App.Close();
// Escape key : exit
if ((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Key::Escape))
App.Close();
// Resize event : adjust viewport
if (Event.Type == sf::Event::Resized)
glViewport(0, 0, Event.Size.Width, Event.Size.Height);
if ((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Key::F12)) {
time(&rawtime);
timeinfo = localtime(&rawtime);
strftime(buffer, 80, "%X-%d:%m:%Y", timeinfo);
ScreenShotImage = App.Capture();
char location[40] = "datas/Screenshots/";
char end[5] = ".jpg";
strncat(location, buffer, 40);
strncat(location, end, 5);
ScreenShotImage.SaveToFile(location);
printf("Screenshot saved!\n");
}
}
if (App.GetInput().IsKeyDown(sf::Key::Q)) {
xrot += 0.1;
if(xrot > 360) xrot -= 360;
printf("q was pressed!");
}
if (App.GetInput().IsKeyDown(sf::Key::Z)) {
xrot -= 0.1;
if(xrot < -360) xrot += 360;
printf("z was pressed!");
}
if (App.GetInput().IsKeyDown(sf::Key::W)) {
float xrotrad, yrotrad;
yrotrad = (yrot / 180 * 3.141592654f);
xrotrad = (xrot / 180 * 3.141592654f);
xpos += float(sin(yrotrad));
zpos -= float(cos(yrotrad));
ypos -= float(sin(xrotrad));
printf("w was pressed!");
}
if (App.GetInput().IsKeyDown(sf::Key::S)) {
float xrotrad, yrotrad;
yrotrad = (yrot / 180 * 3.141592654f);
xrotrad = (xrot / 180 * 3.141592654f);
xpos -= float(sin(yrotrad));
zpos += float(cos(yrotrad));
ypos += float(sin(xrotrad));
printf("s was pressed!");
}
if (App.GetInput().IsKeyDown(sf::Key::D)) {
yrot += 0.1;
if (yrot > 360) yrot -= 360;
printf("d was pressed!");
}
if (App.GetInput().IsKeyDown(sf::Key::A)) {
yrot -= 0.1;
if (yrot < -360) yrot += 360;
printf("a was pressed!");
}
// Set the active window before using OpenGL commands
// It's useless here because active window is always the same,
// but don't forget it if you use multiple windows or controls
App.SetActive();
// Clear color and depth buffer
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
camera();
glEnable (GL_DEPTH_TEST); //enable the depth testing
glEnable (GL_LIGHTING); //enable the lighting
glEnable (GL_LIGHT0); //enable LIGHT0, our Diffuse Light
glShadeModel (GL_SMOOTH); //set the shader to smooth shader
// Apply some transformations
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(0.f, 0.f, -200.f);
glRotatef(Clock.GetElapsedTime() * 50, 1.f, 0.f, 0.f);
glRotatef(Clock.GetElapsedTime() * 30, 0.f, 1.f, 0.f);
glRotatef(Clock.GetElapsedTime() * 90, 0.f, 0.f, 1.f);
// Draw a cube
glBegin(GL_TRIANGLES);
glTexCoord2f(0,0); glVertex3f(-50.f, -50.f, 0.f);
glTexCoord2f(0,1); glVertex3f(-50.f, 50.f, 0.f);
glTexCoord2f(1,1); glVertex3f( 50.f, 50.f, 0.f);
glEnd();
sf::String Text("Welcome to Torem Online 0.0.1");
Text.SetPosition(10.f, 10.f);
Text.SetColor(sf::Color(128,0, 128));
App.Draw(Text);
// Finally, display rendered frame on screen
App.Display();
}
glDeleteTextures(1, &Texture);
return EXIT_SUCCESS;
}