1
Graphics / Re: Nothing in Graphics module compiles!
« on: January 29, 2015, 06:32:32 am »
So, it turns out I was retarded and linked to "sfml-graphis"
It was my grandma who noticed.
It was my grandma who noticed.
This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.
Do NOT use the default font. Last I checked, it's completely broken and results in errors. It might just be a specific version of SFML, though.
maybe it's the copy that is broken, try to get a reference on the default font instead of copying it
GButton.h
/////////////////////
/// The font that the button class will use
/////////////////////
const sf::Font *font;
GButton.cpp
GButton::GButton(sf::RenderWindow& window, std::string text, sf::Color selectedColor, sf::Color deselectedColor)
{
//set the render window
this->window = &window;
//set the colors
this->selectedColor = selectedColor;
this->deselectedColor = deselectedColor;
//reference to the default font
font = &sf::Font::GetDefaultFont();
this->text = sf::String(text, *font, 30);
}
GButton.cpp
void GButton::Draw()
{
window->Draw(text);
}
sf::Font MyFont = sf::Font::GetDefaultFont();
// Create a graphical string
sf::String Hello;
Hello.SetText("Hello !\nHow are you ?");
Hello.SetFont(MyFont);
Hello.SetColor(sf::Color(0, 128, 128));
Hello.SetPosition(100.f, 100.f);
Hello.SetRotation(15.f);
Hello.SetSize(50.f);
this->selectedColor = sf::Color::Green;
this->deselectedColor = sf::Color::Red;
sfml-graphics.lib
sfml-window.lib
sfml-system.lib
sfml-audio.lib
sfml-main.lib
opengl32.lib
glu32.lib
Quote1st, the SFML window I create does not draw/display anything until I move the mouse over it.
Normal, your drawing code is inside the event loop. So it won't be executed until an event occurs.
///////////////////////////////////
/// Main starts the program,
/// creates a GameManager and
/// has it loop until completion
///////////////////////////////////
int main(int argc, char** argv)
{
//constructor game manager and start looping
GameManager Manager("Template");
Manager.Loop();
}
GameManager::GameManager(string title)
{
//the dimentions of the window
int size[2] = {800, 600} ;
//creates the game window, sized to the two elements of the array declared above
window.Create(sf::VideoMode(size[0],size[1]), title);
this->initGL();
this->resize(size[0], size[1]);
}
GameManager::~GameManager(void)
{
}
const sf::Input& GameManager::GetInput()
{
return window.GetInput();
}
void GameManager::Loop()
{
//constantly loop through the window
while(window.IsOpened())
{
//go through the event processing loop
sf::Event Event;
while(window.GetEvent(Event))
{
//process event based on type
switch(Event.Type)
{
//resize if resized
case sf::Event::Resized:
resize(Event.Size.Width, Event.Size.Height);
break;
//close if closed
case sf::Event::Closed:
window.Close();
break;
}
this->update();
this->draw();
window.Display();
}
}
}
void GameManager::draw()
{
//clear the screen
glClear(GL_COLOR_BUFFER_BIT);
glMatrixMode(GL_PROJECTION);
//glLoadIdentity();
gluLookAt(0,0,1, 0,0,0, 0,1,0);
//viewport code here
//////////////////////
//////////////////////
//////////////////////
///end viewport code
//Drawing code here
glMatrixMode(GL_MODELVIEW);
//glLoadIdentity();
//for demo purposes, we draw a triangle
glPointSize(100);
glBegin(GL_TRIANGLES);
{
glVertex2f(-1,0);
glVertex2f(1,0);
glVertex2f(0,1);
}
glEnd();
//end drawing code
glFlush();
}
void GameManager::update()
{
}
void GameManager::initGL()
{
glClearColor(0,0,1,0);
glColor3f(0,1,0);
}
void GameManager::resize(float w, float h)
{
glViewport(0,0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(60, w /h, 1, 200);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(0,0,5, 0,0,0, 0,1,0);
}
class GameManager
{
public:
//////////////////
/// Creates the game window
/// And calls the resize method to properly set the window attributes and glViewport
/// \param The title of the game's window
//////////////////
GameManager(string title);
///////////////////
///
//////////////////
~GameManager(void);
//////////////////
/// Has the GameManager loop through the window cycle,
/// as well as updating and drawing stuff
//////////////////
void Loop();
///////////////////////////
/// Gets a reference to the window's input struct
///////////////////////////
const sf::Input& GetInput();
private:
////////////////////////
/// The render window
////////////////////////
sf::RenderWindow window;
//////////////////
/// The portion of the game currently running.
/// IE: MainMenu, OptionsMenu, etc
//////////////////
GameState state;
///////////////
/// Initializes the openGL states
//////////////
void initGL();
/////////////
/// Draws the game
/////////////
void draw();
////////////////
/// Updates everythign in need of updating
///////////////
void update();
///////////////
/// Resizes the window
///////////////
void resize(float w, float h);
};
#include <GL/freeglut.h>
#include <SFML/Window.hpp>
static GLfloat year = 0, day = 0, moon = 0;
bool m,M,y,Y,d,D;
void init()
{
glClearColor(0,0,0,0);
glShadeModel(GL_FLAT);
glEnable(GL_DEPTH_TEST);
}
///////////////////
///Display the sun, earth, and moon
///////////////////
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glColor3f(1,1,1);
glColor3f(1,1,0);
glPushMatrix();
glutSolidSphere(1, 20, 16);//the sun
//here is where we transform matrices to get to the planet's position
glColor3f(0,0,1);
glRotatef((GLfloat)year , 0, 1, 0);
glTranslatef(2,0,0);
glPushMatrix(); //remember the planets position
glRotatef((GLfloat)day, 0, 1, 0); //rotate the planet, but do not remember its rotation
glutSolidSphere(0.2, 10, 8); // planet
glColor3f(0.3,0.3,0.3);
glPopMatrix(); //go back to the position at which the earth was drawn (before rotation)
glRotatef(moon, 0, 1.5, 0);
glTranslatef(0.5,0,0);
glutSolidSphere(0.05,10,8); //and then draw the moon, transformed properly
glPopMatrix();
glPopMatrix();
//glutSwapBuffers();
//glutPostRedisplay();
}
////////////////////////////
///Set all the viewport and perspective stuff when the window is resized
////////////////////////////
void reshape(int w, int h)
{
glViewport(0,0, (GLsizei)w, (GLsizei)h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(60, (GLfloat)w / (GLfloat)h, 1, 200);
gluLookAt(0,2, 5, 0,0,0, 0, 1, 0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
void update(const sf::Input &input)
{
const GLfloat rotationSpeed = 0.05f;
//if M is pressed, move the moon forwards or backwards in rotation
if(input.IsKeyDown(sf::Key::M) && !input.IsKeyDown(sf::Key::LShift))
moon = (moon + 2 * rotationSpeed);
else if(input.IsKeyDown(sf::Key::M))
moon = (moon - 2 * rotationSpeed);
//if Y is pressed move the earth in orbit
if(input.IsKeyDown(sf::Key::Y) && !input.IsKeyDown(sf::Key::LShift))
year = (year + rotationSpeed);
else if(input.IsKeyDown(sf::Key::Y))
year = (year - rotationSpeed);
//if D is pressed rotate earth, simulating days
if(input.IsKeyDown(sf::Key::D) && !input.IsKeyDown(sf::Key::LShift))
day = (day + rotationSpeed);
else if(input.IsKeyDown(sf::Key::D))
day = (day - rotationSpeed);
}
////////////////////////////////////////////////
/// /param: Event: The event
/// /param: capitalValue: The value of the capital lettered bools
/// /param: lowerCaseValue: The value of the lowercase bools
////////////////////////////////////////////////
void keyboard(sf::Event theEvent, bool value)
{
switch(theEvent.Key.Code)
{
case sf::Key::Y:
if(theEvent.Key.Shift)
Y = value;
else
y = value;
break;
}
}
int main(int argc, char** argv)
{
init();
glutInit(&argc, argv); //so glutSolidSphere can be used
sf::Window *app = new sf::Window(sf::VideoMode(800,600), "SFML",
sf::Style::Close);
reshape(app->GetWidth(), app->GetHeight()); //set the proper window and viewport stuff
while(app->IsOpened())
{
sf::Event theEvent;
while(app->GetEvent(theEvent))
{
switch(theEvent.Type)
{
case sf::Event::KeyPressed:
keyboard(theEvent, true);
break;
case sf::Event::KeyReleased:
keyboard(theEvent, false);
break;
case sf::Event::Resized:
reshape(app->GetWidth(), app->GetHeight());
break;
case sf::Event::Closed:
app->Close();
break;
}
}
update(app->GetInput()); //update the planets based on current keyboard status
display(); //display the planets
app->Display(); //show the window
}
}