martedì 28 agosto 2012

Creating a Qt application in c++ in Windows

Today i'm posting about the famous GUI library created by nokia: Qt.
This  library provide a cross-platform application and UI framework.
To start programming with this library in windows we need to install first minGW (gcc ported to windows), you can download it here.
At the end of the installation add the minGW/bin folder to your PATH variable, to do this go to
control panel>system>advanced system settings>advanced>environment variables

then select the PATH variable in  the system variables and click edit and add at the end

;C:\MinGW\bin

now to check if everything it's working open the command line and try writing

make

If it's recognized as a command you've done everything right else try checking this tutorial for the installation.
Now we need to install the Qt library that you can download here.
When you finished downloading, install the library and add the    C:\Qt\4.8.2\bin
to the environment variables just like we have done with the minGW binaries.
When you've done check if everything is working writing in the command line

qmake

To start programming create file main.cpp and write this code:

#include <Qt/QApplication.h>
#include <Qt/QWidget.h>

int main(int argc, char *argv[]){
    QApplication app(argc, argv);

    QWidget window;

    window.resize(250, 150);
    window.setWindowTitle("Example");
    window.show();

    return app.exec();
}

Now open a command line and move to the folder that contains the file using cd command and then write this in the command line to compile the file:

qmake -project
qmake
make

If there's no error in your code or in the environment variables you will find the compiled file in the debug folder.

I hope you like this post, if you have any question please comment or send me an e-mail to damianoandre@gmail.com

Bye, Dami

giovedì 9 agosto 2012

Simple game/physics engine in Processing

Today i'm posting about a simple physics engine that I made in processing, I think there are some bugs but it's good to start a 2D game engine.
Here is the code:
Block b1;
Rectc c;
Boolean collision = false;
Boolean jumping = false;

void setup(){
  size(600, 400);
  c = new Rectc(40.0, float(height)-100);
  b1 = new Block(20.0, float(height)-50);

  }

void draw(){
  background(255);
  c.update();
  b1.update();
  println(jumping);
  }

class Block{
  float xpos, ypos;
  Block(float x, float y){
    xpos = x;
    ypos = y;
    }
  void update(){
    fill(255, 0, 0);
    rect(xpos, ypos, 50, 50);
    if(c.ypos >= ypos-20 && (c.xpos+10 >= xpos && c.xpos+10 <= xpos+50)){
      collision = true;
      jumping = false;
      }
    else{
      collision = false;
      }
    }
  }

class Rectc{
  float xpos, ypos;
  float vx = 0;
  float vy = 0;
  float elasticy = 0.4;
  Rectc(float x, float y){
    xpos = x;
    ypos = y;
    }
  void update(){
    if(keyPressed){
      if(jumping != true && key =='w'){
        vy += -15;
        jumping = true;
      }
      if(key =='a'){
        vx += -1;
        }
      if(key == 'd'){
        vx += 1;
        }
      if(key == 's'){
        vy += 0.25;       
        }
    }
    xpos += vx;
    ypos += vy;
    vx *= 0.4;
    vy *= 0.99;
    vy += 0.25;
    if(collision){
      vy = -Math.abs(vy);
      vy *= elasticy;
      ypos += vy;
      }
    fill(0, 255, 0);
    rect(xpos, ypos, 20, 20);
    }
  }

The code is available for download here(.zip).
If you have any questions about the code please comment or send me an e-mail to damianoandre@gmail.com

I hope you enjoy this post!
Bye, Dami