SFML for Codeblocks Setup Tutorial

What is SFML?

SFML is a simple to use and portable API written in C++. You can think of it as an object oriented SDL. SFML is made of modules in order to be as useful as possible for everyone. You can use SFML as a minimalist window system in order to use OpenGL, or as a complete multimedia library full of features to build video games or multimedia softwares.

Homepage: http://www.sfml-dev.org/

Installation:

This page provides detailed installation steps. But here i will try to explain.

  1. Since we are setting up codeblocks download the SFML TDM(SJLJ) version. Download 32 bit if you want maximum support. 32 bits run on both 32 and 64 bit systems.
  2. Now unzip and keep in your folder of choice. It’s best to use a path like, c:\sfml\
  3. Go to Settings -> Compiler -> Search Directories -> compiler ( sub tab ) then click Add. Now locate the folder where you unzipped sfml and find the include folder. For example, C:\SFML-2.2\include 

    compiler settings
    compiler settings
  4. Next goto Linker ( sub tab ) click Add and find the lib folder of unzipped sfml. For example, C:\SFML-2.2\lib

    linker lib
    linker lib
  5. Now open codeblocks. Go to Settings -> Compiler -> Linker Settings we can see anadd button here. If we click it we are shown .a files.

    linker settings
    linker settings
  6. File names are prefixed with lib also with .a extension. We do not need that. For example, if the library you want to add is named as, libsfml-graphics.a we can just type in sfml-graphics after clicking on add button. We must omit lib and .a part in the name.
  7. We also need to maintain order of libraries. For example, we add sfml-graphics, sfml-window, then sfml-system.
  8. Also if you are wondering what those .a files with -s-d stand for. -s stands for static and -d stands for debug. I’ve setup the version with out static. But if want to use static linkers settings for example, sfml-graphics-s then we need another step. Goto compiler settings tab, then goto #defines sub tab and write the following, SFML_STATIC.
  9. Now a crucial step when you create a project or a file copy all the dll files located in your unzipped sfml folder bin directory to your executable directory. Means basically copy dll files to where your cpp file is located. 

    add dll to cpp location
    add dll to cpp location
  10. Now create a cpp file and copy the codes located below this page to your cpp and Run. If it runs then you have successfully setup sfml.  Now start creating apps and games. Happy coding 🙂

 

Advertisements

UVA Problem 483 (Word Scramble) Solution

UVA Problem 483 (Word Scramble) Solution:


Click here to go to this problem in uva Online Judge.

Solving Technique:

This problem asks us to reverse each words in a given sentence. There may be other better and faster solutions. I’ve implemented my solution using stack. First we are looking for space or null character. If those are not found we keep pushing characters in stack until we find space or null character. While doing that we also keep a count of number of items we pushed in the stack. So now if we find a space character or a null character we keep popping and printing the items the number of times pushed. We also check to see if we reached the last character in which case we don’t print any space character.

Important: We should not reverse the whole string but only each words. Also make sure there are no trailing space ( I got Presentation Error (PE) for this ) after the end of sentence. Also there should not be extra blank lines.


Critical Input:

I love you.
You love me.
We're a happy family.

Critical Output:

I evol .uoy
uoY evol .em
er'eW a yppah .ylimaf

Code:

/*
 * @author Quickgrid ( Asif Ahmed )
 * @link   https://quickgrid.wordpress.com
 */

#include<stdio.h>

int top = -1;
char st[1000], sentence[1000];

void push(char c){
    st[++top] = c;
}

void pop(){
    printf("%c", st[top]);
    st[top--] = '\0';
}

int main(){
    int c = 0;
    
    while (gets(sentence)){
        for (int i = 0; sentence[i]; ++i){
            if (sentence[i + 1] == '\0' || sentence[i] != ' '){
                push(sentence[i]);
                ++c;
            }
            if (sentence[i] == ' ' || sentence[i + 1] == '\0'){
                for (int j = 0; j < c; ++j){
                    pop();
                }
                c = 0;
                if(sentence[i + 1] != '\0'){
                    printf(" ");
                }
            }
        }
        printf("\n");
    }

    return 0;
}

Console based Dice Game Code C

A Console based turn based dice rolling game. The player with highest number on dice wins. If both have same dice then the game draws.

CODE:

#include<stdio.h>
#include<time.h>
#include<stdbool.h>

#define TOTAL_PLAYERS 2

int rollDices(int sides);
int tossCoin();
void exitMessage();
void invalidOp();
void playerDiceOptions(int tc, int sides);

struct player
{
    char name[100];
    bool played;
    int dice;
} p[TOTAL_PLAYERS];


int main()
{
    int sides = 6;
    char tcAgree = '\0';


    for(int i=0; i<TOTAL_PLAYERS; i++)
    {
        printf("Enter Player %d name:\n", i+1);
        scanf("%s", &p[i].name);
    }


    int tc = tossCoin();
    printf("Toss COIN: (Y/N)?\n");
    getchar();
    tcAgree = getchar();


    printf("\n");
    if(tcAgree - 'y'==0|| tcAgree - 'Y'==0)
    {
        printf("Player %d \"%s\" will play first\n", tc+1, p[tc].name);
        p[tc].played = true;
    }
    else if(tcAgree - 'n'==0 || tcAgree - 'N'==0)
    {
        exitMessage();
        return 0;
    }
    else
    {
        invalidOp();
        return 0;
    }

    playerDiceOptions(tc, sides);


    //invert the player
    if(tc==0)
        tc=1;
    else
        tc=0;

    printf("\n");
    for(int i=0; i<TOTAL_PLAYERS; i++)
    {
        if(!p[i].played)
        {
            printf("Player %d \"%s\" will play\n", i+1, p[i].name);
            playerDiceOptions(tc, sides);
        }
    }

    printf("\n");
    Sleep(2000);

    int k=0;
    if(p[k].dice>p[k+1].dice)
        printf("Player %d \"%s\" wins\n", k+1, p[k].name);
    else if(p[k].dice<p[k+1].dice)
        printf("Player %d \"%s\" wins\n", (k+1)+1, p[k+1].name);
    else
        printf("the game is a draw\n");

    return 0;
}

int rollDices(int sides)
{
    int dice1 = rand() % sides + 1;
    return dice1;
}

int tossCoin()
{
    srand(time(NULL));
    return rand() % 2;
}

void exitMessage()
{
    printf("Exiting.....");
    Sleep(2000);
}

void invalidOp()
{
    printf("Invalid Operation\n");
}

void playerDiceOptions(int tc, int sides)
{
    char rollAgree = '\0';

    printf("Roll dice: (Y/N)\n");
    getchar();
    rollAgree = getchar();
    if(rollAgree - 'y'==0|| rollAgree - 'Y'==0)
    {
        p[tc].dice = rollDices(sides);
        printf("Player %d \"%s\" got on dice: %d\n", tc+1, p[tc].name, p[tc].dice);
    }
    else if(rollAgree - 'n'==0 || rollAgree - 'N'==0)
    {
        printf("I get it you don't want to play\n");
        exitMessage();
        return 0;
    }
    else
    {
        printf("Invalid operation\n");
        return 0;
    }
}

Factorization Code C

Sample Input:

48
18

Sample Output:

1 * 48
2 * 24
3 * 16
4 * 12
6 * 8
1 * 18
2 * 9
3 * 6

CODE:

#include<stdio.h>
#include<math.h>

int main(){
   int n=0;

   printf("Enter number to find factorization:\n");
   scanf("%d", &n);

   printf("Factorization:\n");
   for(int i=1; i<=sqrt(n); i++){
      if(n%i==0)
          printf("%d * %d\n", i, n/i);
   }
   return 0;
}

Prime Number Check Primality Test Code C

Sample Input:

8009
800
-99
1
0

Sample Output:

Prime
Not Prime
Not Prime
Not Prime
Not Prime

CODE:

#include<stdio.h>
#include<math.h>

int main()
{
    int i,j,n;
    i=j=n=0;

//
    printf("Enter a number to check if prime:\n");
    scanf("%d", &n);

//
    if(n<=1)
    {
        printf("Not prime\n");
        return 0;
    }

    for(i=2; i<=sqrt(n); i++)
    {
        if(n%i==0)
        {
            printf("Not prime\n");
            return 0;
        }
    }

    printf("prime\n");

    return 0;
}

Console based Static Clock with Structure C

Create a 24 hour clock with struct that takes hour, minutes, seconds as input. First check if inputs are valid. The output is 1 second plus the input. Input format is (H M S) hour, minute then second.

Input:

2 3 9
4 2 59
8 59 59
23 59 59

Output:

2 : 3 : 10
4 : 3 : 0
9 : 0 : 0
0 : 0 : 0

CODE:

#include<stdio.h>

struct time{
 short _h, _m, _s;
} t;

int main(){
   while(1){
       printf("Enter Hour, Minute, Second:\n");
       scanf("%d %d %d", &t._h, &t._m, &t._s);
       if(t._h<0 || t._h>23 || t._m<0 || t._m>60 || t._s<0 || t._s>60)
           printf("\nWrong input try again!\n\n");
       else break;
       printf("\n");

       if(++t._s == 60)
           t._s = 0;
       if(++t._m == 60)
           t._m = 0;
       if(++t._h == 24)
           t._h = 0;
    }
    printf("%d : %d : %d\n", t._h, t._m, t._s);
    return 0;
}

Hexadecimal to Decimal Conversion Code C

Sample Input:

F7B

Sample Output:

3963

CODE:

#include<stdio.h>
#include<string.h>
#include<math.h>

int main(){
 int sum,x;
 sum=x=0;
 char bin[100]={'\0'};

 gets(bin);
 int len = strlen(bin);
 x = len-1;

 for(int k=0; k<len; k++){
   if(bin[k]-'0'>=0 && bin[k]-'0'<=9){
     sum += (bin[k]-'0') * pow(16, x);
     --x;
   }else if(bin[k]>='A' && bin[k]<='F'){
     sum += (bin[k]-55) * pow(16, x);
     --x;
   }else{
     printf("Invalid input");
     return 0;
   }
 }

 printf("%d", sum);
 return 0;
}