pls post here tricks that you know of in C/C++, thanks

This is a discussion on pls post here tricks that you know of in C/C++, thanks within the C++ Programming forums, part of the General Programming Boards category; Why don't you ask them to write your entire website for you while your at it too?...

  1. #16
    Unregistered
    Guest
    Why don't you ask them to write your entire website for you while your at it too?

  2. #17
    Linguistic Engineer... doubleanti's Avatar
    Join Date
    Aug 2001
    Location
    CA
    Posts
    2,459
    >buffer[3] = 'x'; // puts the letter 'x' on the index 3

    3[buffer] = 'x'; // same thing<

    is that so? why weren't we informed of this notation!!! when is that ever true? that's pretty far out...
    hasafraggin shizigishin oppashigger...

  3. #18
    Intranasal Heroin User Xterria's Avatar
    Join Date
    Sep 2001
    Location
    Buffalo, NY
    Posts
    1,035
    that's clever...
    too lazy to try it out, though.

  4. #19
    I'm Back
    Join Date
    Dec 2001
    Posts
    556
    i dont think this is a trick but i see a lot of programmers dont know about this -

    if there is just one statement in for/if etc conditional statements then you dont need the curlies : { }

    ie instead of
    Code:
     for(i=0;i<10;i++)
     {
      cout<<i<" ";
     }
    this
    Code:
     for(i=0;i<10;i++)
      cout<<i<" ";


    Another cool trick = read the help of your compiler and you'll find amazing functions/headers to help you

  5. #20
    Linguistic Engineer... doubleanti's Avatar
    Join Date
    Aug 2001
    Location
    CA
    Posts
    2,459
    say ihsir, yeah that really helps actually... especially with nested scope [ie loops or conditionals etc...]
    hasafraggin shizigishin oppashigger...

  6. #21
    Its not rocket science vasanth's Avatar
    Join Date
    Jan 2002
    Posts
    1,683
    Ok here is a lame trick..

    When you do graphics in c++ (16 bit for dos) Instead of using getimage and stuff you can draw an image and when you want it just move it and overdraw the previous one with black or the backgrounf colour... Using a loop.

    Below is an entire game pack done with this simple logic..




    # include <process.h>
    # include <dos.h>
    #include <graphics.h>
    #include <stdlib.h>
    #include <stdio.h>
    #include <conio.h>
    # include <iostream.h>
    int i,k,y=180,select,counter,counter1;
    int lk=1,tic1=0,tic2=0,vas=10,kit=10;



    void inig()
    {

    closegraph();
    int gdriver = DETECT, gmode, errorcode;
    initgraph(&gdriver, &gmode, "");


    }

    void start()
    {
    inig();
    int i;
    setcolor(8);
    for (i=0;i<640;i++)
    { line (i,0,i,15);
    line (i,480,i,465); }

    settextjustify(CENTER_TEXT, CENTER_TEXT);
    settextstyle(GOTHIC_FONT, HORIZ_DIR, 5);
    setcolor(9);
    settextstyle (10,HORIZ_DIR,5);
    outtextxy(300,75,"Gamez Center");
    delay(100);
    setcolor(RED);
    settextstyle (1,HORIZ_DIR,5);
    outtextxy(310,28, " X ");
    setcolor(6);
    settextstyle (8,HORIZ_DIR,2);
    outtextxy(300,31, " Group ");

    settextstyle (13,HORIZ_DIR,2);
    outtextxy(310,39, " Presents ");

    settextstyle (8,HORIZ_DIR,2);
    setcolor(27);
    outtextxy(300,175, "1) Tic Tac Toe");
    settextstyle (8,HORIZ_DIR,2);
    outtextxy(300,210, "2) Battle Ship");
    outtextxy(300,245, "3) Run Man ");
    outtextxy(300,280, "4) Snake ");
    outtextxy(300,315, "5) Credits ");
    outtextxy(300,350, "6) Exit ");

    }

















    void circ(int cho)
    {
    setcolor(GREEN);
    if(cho==1)
    circle(200,100,30);
    if(cho==2)
    circle(300,100,30);
    if(cho==3)
    circle(400,100,30);
    if(cho==4)
    circle(200,200,30);
    if(cho==5)
    circle(300,200,30);
    if(cho==6)
    circle(400,200,30);
    if(cho==7)
    circle(200,300,30);
    if(cho==8)
    circle(300,300,30);
    if(cho==9)
    circle(400,300,30);

    }




    void cross(int tr)
    {
    setcolor(GREEN);
    if(tr==1)
    {
    moveto(200,60);
    lineto(160,140);
    lineto(240,140);
    lineto(200,60);
    }

    if(tr==2)
    {
    moveto(300,60);
    lineto(260,140);
    lineto(340,140);
    lineto(300,60);
    }

    if(tr==3)
    {
    moveto(400,60);
    lineto(360,140);
    lineto(440,140);
    lineto(400,60);
    }

    if(tr==4)
    {
    moveto(200,160);
    lineto(160,240);
    lineto(240,240);
    lineto(200,160);
    }

    if(tr==5)
    {
    moveto(300,160);
    lineto(260,240);
    lineto(340,240);
    lineto(300,160);
    }

    if(tr==6)
    {
    moveto(400,160);
    lineto(360,240);
    lineto(440,240);
    lineto(400,160);
    }

    if(tr==7)
    {
    moveto(200,260);
    lineto(160,340);
    lineto(240,340);
    lineto(200,260);
    }

    if(tr==8)
    {
    moveto(300,260);
    lineto(260,340);
    lineto(340,340);
    lineto(300,260);
    }

    if(tr==9)
    {
    moveto(400,260);
    lineto(360,340);
    lineto(440,340);
    lineto(400,260);
    }




    }


    void win(int wi)
    {
    if(wi==1)
    {
    for(int crs=0;crs<20;crs++)
    {

    moveto(400-crs,60-crs);
    lineto(360-crs,140-crs);
    lineto(440-crs,140-crs);
    lineto(400-crs,60-crs);

    moveto(400-crs,160-crs);
    lineto(360-crs,240-crs);
    lineto(440-crs,240-crs);
    lineto(400-crs,160-crs);

    moveto(400-crs,260-crs);
    lineto(360-crs,340-crs);
    lineto(440-crs,340-crs);
    lineto(400-crs,260-crs);

    delay(10);


    }

    }

    if(wi==2)
    {
    for(int crs=0;crs<20;crs++)
    {
    moveto(200-crs,260-crs);
    lineto(160-crs,340-crs);
    lineto(240-crs,340-crs);
    lineto(200-crs,260-crs);

    moveto(300-crs,260-crs);
    lineto(260-crs,340-crs);
    lineto(340-crs,340-crs);
    lineto(300-crs,260-crs);

    moveto(400-crs,260-crs);
    lineto(360-crs,340-crs);
    lineto(440-crs,340-crs);
    lineto(400-crs,260-crs);
    delay(10);


    }

    }


    if(wi==3)
    {
    for(int crs=0;crs<20;crs++)
    {
    moveto(400-crs,60-crs);
    lineto(360-crs,140-crs);
    lineto(440-crs,140-crs);
    lineto(400-crs,60-crs);

    moveto(300-crs,160-crs);
    lineto(260-crs,240-crs);
    lineto(340-crs,240-crs);
    lineto(300-crs,160-crs);

    moveto(200-crs,260-crs);
    lineto(160-crs,340-crs);
    lineto(240-crs,340-crs);
    lineto(200-crs,260-crs);
    delay(10);




    }





    }

    if(wi==5)
    {
    for(int crs=0;crs<20;crs++)
    {
    moveto(200-crs,60-crs);
    lineto(160-crs,140-crs);
    lineto(240-crs,140-crs);
    lineto(200-crs,60-crs);

    moveto(200-crs,160-crs);
    lineto(160-crs,240-crs);
    lineto(240-crs,240-crs);
    lineto(200-crs,160-crs);

    moveto(200-crs,260-crs);
    lineto(160-crs,340-crs);
    lineto(240-crs,340-crs);
    lineto(200-crs,260-crs);

    delay(10);

    }
    }


    if(wi==6)
    {
    for(int crs=0;crs<20;crs++)
    {
    moveto(200-crs,60-crs);
    lineto(160-crs,140-crs);
    lineto(240-crs,140-crs);
    lineto(200-crs,60-crs);

    moveto(300-crs,160-crs);
    lineto(260-crs,240-crs);
    lineto(340-crs,240-crs);
    lineto(300-crs,160-crs);

    moveto(400-crs,260-crs);
    lineto(360-crs,340-crs);
    lineto(440-crs,340-crs);
    lineto(400-crs,260-crs);

    delay(10);

    }
    }


    setcolor(RED);
    settextstyle (8,HORIZ_DIR,3);
    outtextxy(100,410, "The Computer Wins The Game");
    delay(3000);
    closegraph();
    system("game.exe");

    }


    void ticlogic()
    {
    int x,y,x1,y1;
    x=200;
    y=100;
    cross(9);
    prog:
    setcolor(GREEN);
    circle(x,y,10);
    x1=x;
    y1=y;

    select=getche();
    setcolor(BLACK);
    circle(x1,y1,10);
    if(select==0x4d)
    {
    x=x+100;
    if(x>400)
    x=200;
    }
    if(select==0x4b)
    {
    x=x-100;
    if(x<200)
    x=400;
    }

    if(select==0x48)
    {
    y=y-100;
    if(y<100)
    y=300;
    }

    if(select==0x50)
    {
    y=y+100;
    if(y>300)
    y=100;

    }

    if(select==27)
    exit(0);

    if(select==13 && vas==10)
    {
    if(x==400 && y==100)
    {
    kit=153;
    lk=131;
    tic1=432;
    tic2=412;
    circ(3);
    delay(1000);
    cross(7);

    }
    if(kit==153 && x==200 && y==100)
    {
    circ(1);
    delay(1000);
    cross(7);
    win(2);

    }
    if(kit==153 && x==300 && y==100)
    {
    circ(2);
    delay(1000);
    cross(7);
    win(2);

    }
    if(kit==153 && x==200 && y==200)
    {
    circ(4);
    delay(1000);
    cross(7);
    win(2);

    }
    if(kit==153 && x==300 && y==200)
    {
    circ(5);
    delay(1000);
    cross(7);
    win(2);

    }
    if(kit==153 && x==400 && y==200)
    {
    circ(6);
    delay(1000);
    cross(7);
    win(2);

    }

    if(kit==153 && x==300 && y==300)
    {
    circ(8);
    delay(1000);
    cross(1);
    kit=323;
    }
    if(kit==323 && x==300 && y==100)
    {
    circ(2);
    delay(1000);
    cross(4);
    win(5);
    }
    if(kit==323 && x==200 && y==200)
    {
    circ(4);
    delay(1000);
    cross(5);
    win(6);
    }

    if(kit==323 && x==300 && y==200)
    {
    circ(5);
    delay(1000);
    cross(4);
    win(5);
    }
    if(kit==323 && x==400 && y==200)
    {
    circ(6);
    delay(1000);
    cross(4);
    win(5);
    }

    }










    if(select==13)
    {
    vas==223;
    if(x==300 && y==100 && kit==10)
    {
    circ(2);
    delay(1000);
    lk=0;
    cross(3);
    tic1=10;

    }

    if(tic2==10)
    {
    if(x==300 && y==200)
    {
    circ(5);
    delay(1000);
    cross(8);
    win(2);


    }
    if(x==300 && y==300)
    {
    circ(8);
    delay(1000);
    cross(5);
    win(3);

    }
    if(x==200 && y==100)
    {
    circ(1);
    delay(1000);
    cross(5);
    win(3);

    }
    if(x==200 && y==200)
    {
    circ(4);
    delay(1000);
    cross(5);
    win(3);


    }


    }






    if(tic1==10 && tic2==0)
    { vas=313;


    if(x==200 && y==100)
    {
    circ(1);
    delay(1000);
    cross(6);
    win(1);

    }
    if(x==200 && y==200)
    {
    circ(4);
    delay(1000);
    cross(6);
    win(1);

    }
    if(x==300 && y==200)
    {
    circ(5);
    delay(1000);
    cross(6);
    win(1);

    }
    if(x==200 && y==300)
    {
    circ(7);
    delay(1000);
    cross(6);
    win(1);

    }
    if(x==300 && y==300)
    {
    circ(8);
    delay(1000);
    cross(6);
    win(1);

    }


    if(x==400 && y==200)
    {
    circ(6);
    delay(1000);
    cross(7);
    tic2=10;
    }


    }



    if(lk==1 && x==200 && y==100 && tic2==0)
    { vas=434;
    circ(1);
    delay(1000);
    cross(3);
    counter=1;
    }
    prog1:
    if(counter==1)
    { vas=422;

    if(x==400 && y==200)
    {

    circ(6);
    delay(1000);
    cross(7);
    counter=10;
    goto prog1;
    }


    if(x==300 && y==100)
    {
    circ(2);
    delay(1000);
    cross(6);
    win(1);
    }

    if(x==200 && y==200)
    {circ(4);
    delay(1000);
    cross(6);
    win(1);
    }
    if(x==300 && y==200)
    {
    circ(5);
    delay(1000);
    cross(6);
    win(1);
    }
    if(x==200 && y==300)
    {
    circ(7);
    delay(1000);
    cross(6);
    win(1);
    }
    if(x==300 && y==300)
    {
    circ(8);
    delay(1000);
    cross(6);
    win(1);


    }}
    if(counter==10)
    { vas=4423;

    if(x==300 && y==100)
    {
    circ(2);
    delay(1000);
    cross(8);
    win(2);


    }

    if(x==200 && y==200)
    {
    circ(4);
    delay(1000);
    cross(8);
    win(2);
    }

    if(x==300 && y==200)
    {
    circ(5);
    delay(1000);
    cross(8);
    win(2);
    }
    if(x==300 && y==300)
    {
    circ(8);
    delay(1000);
    cross(5);
    win(3);


    }

    }

    }


    goto prog;


    }



    void doub()
    {

    closegraph();
    char nam1[' '],nam2[' '];
    inig();
    settextstyle(7, HORIZ_DIR, 2);
    setcolor(GREEN);
    rectangle(1,479,639,1);
    rectangle(2,478,638,2);
    setcolor(8);
    outtextxy(60,75, "Enter the first players name >> ");
    gotoxy(60,6);
    cin>>nam1;
    outtextxy(60,105, "Enter the second players name >> ");
    gotoxy(60,8);
    cin>>nam2;
    inig();
    setcolor(GREEN);
    rectangle(1,479,639,1);
    rectangle(2,478,638,2);
    setcolor(BLUE);
    line(150,150,450,150);
    line(150,250,450,250);
    line(250,50,250,350);
    line(350,50,350,350);
    line(150,151,450,151);
    line(150,251,450,251);
    line(251,50,251,350);
    line(351,50,351,350);
    settextstyle(8, HORIZ_DIR, 2);
    setcolor(GREEN);
    outtextxy(60,375,nam1);
    outtextxy(200,375,"to play");

    int xq=200,yq=100;
    int getv;
    si:
    setcolor(GREEN);
    circle(xq,yq,10);
    getv=getche();
    setcolor(BLACK);
    circle(xq,yq,10);

    if(getv==0x4b)
    {
    xq=xq-100;
    if(xq<200)
    xq=400;
    }
    if(getv==0x4d)
    {
    xq=xq+100;
    if(xq>400)
    xq=200;
    }
    if(getv==0x48)
    {
    yq=yq-100;
    if(yq<100)
    yq=300;
    }
    if(getv==0x50)
    {
    yq=yq+100;
    if(yq>300)
    yq=100;
    }
    if(getv==27)
    exit(0);
    if(getv==13 && xq==200 && yq==100)
    {
    circ(1);
    setcolor(BLACK);
    outtextxy(60,375,nam1);
    outtextxy(200,375,"to play");
    setcolor(GREEN);
    outtextxy(60,375,nam2);
    outtextxy(200,375,"to play");

    }
    if(getv==13 && xq==300 && yq==100)
    {
    circ(2);
    setcolor(BLACK);
    outtextxy(60,375,nam1);
    outtextxy(200,375,"to play");
    setcolor(GREEN);
    outtextxy(60,375,nam2);
    outtextxy(200,375,"to play");

    }

    if(getv==13 && xq==400 && yq==100)
    {
    circ(3);
    setcolor(BLACK);
    outtextxy(60,375,nam1);
    outtextxy(200,375,"to play");
    setcolor(GREEN);
    outtextxy(60,375,nam2);
    outtextxy(200,375,"to play");

    }

    if(getv==13 && xq==200 && yq==200)
    {
    circ(4);
    setcolor(BLACK);
    outtextxy(60,375,nam1);
    outtextxy(200,375,"to play");
    setcolor(GREEN);
    outtextxy(60,375,nam2);
    outtextxy(200,375,"to play");

    }

    if(getv==13 && xq==300 && yq==200)
    {
    circ(5);
    setcolor(BLACK);
    outtextxy(60,375,nam1);
    outtextxy(200,375,"to play");
    setcolor(GREEN);
    outtextxy(60,375,nam2);
    outtextxy(200,375,"to play");

    }

    if(getv==13 && xq==400 && yq==200)
    {
    circ(6);
    setcolor(BLACK);
    outtextxy(60,375,nam1);
    outtextxy(200,375,"to play");
    setcolor(GREEN);
    outtextxy(60,375,nam2);
    outtextxy(200,375,"to play");

    }

    if(getv==13 && xq==200 && yq==300)
    {
    circ(7);
    setcolor(BLACK);
    outtextxy(60,375,nam1);
    outtextxy(200,375,"to play");
    setcolor(GREEN);
    outtextxy(60,375,nam2);
    outtextxy(200,375,"to play");

    }

    if(getv==13 && xq==300 && yq==300)
    {
    circ(8);
    setcolor(BLACK);
    outtextxy(60,375,nam1);
    outtextxy(200,375,"to play");
    setcolor(GREEN);
    outtextxy(60,375,nam2);
    outtextxy(200,375,"to play");

    }

    if(getv==13 && xq==400 && yq==300)
    {
    circ(9);
    setcolor(BLACK);
    outtextxy(60,375,nam1);
    outtextxy(200,375,"to play");
    setcolor(GREEN);
    outtextxy(60,375,nam2);
    outtextxy(200,375,"to play");

    }


    goto si;

























    }


    void tictactoe()

    {
    for(int lines=17;lines<500;lines++)
    {
    setcolor(BLACK);
    line(0,lines,800,lines);
    delay(1);
    }

    int abc;
    abc=0;
    pro:
    closegraph();
    inig();
    setcolor(GREEN);
    rectangle(1,479,639,1);
    rectangle(2,478,638,2);
    int jkl,ys=225;
    for(int re=0;re<10;re++)
    {
    setcolor(re);
    circle(450,225,re);
    }
    pro1:
    if(abc==0)
    {
    abc=1;
    setcolor(RED);
    settextjustify(CENTER_TEXT, CENTER_TEXT);
    settextstyle(8, HORIZ_DIR, 2);
    outtextxy(300,175, "TIC TAC TOE");
    setcolor(BLUE);
    outtextxy(300,225, "1) Two Player Mode");
    outtextxy(300,275, "2) Single Player Mode");
    pro2:
    jkl=getche();
    for(int re=0;re<10;re++)
    {
    setcolor(BLACK);
    circle(450,ys,re);
    }


    if(jkl==0x48)
    {
    ys=ys-50;
    if(ys<225)
    ys=275;

    }
    if(jkl==0x50)
    {
    ys=ys+50;
    if(ys>275)
    ys=225;

    }
    if(jkl==27)
    {
    exit(0);
    }
    for(re=0;re<10;re++)
    {
    setcolor(re);
    circle(450,ys,re);
    delay(10);
    }
    int ys1;
    abc=0;
    if(ys==275 && jkl==13)
    {
    closegraph();
    inig();
    goto single;

    }
    if(ys==225 && jkl==13)
    {
    doub();

    }

    goto pro2;

    }
    single:
    setcolor(GREEN);
    rectangle(1,479,639,1);
    rectangle(2,478,638,2);

    setcolor(BLUE);
    line(150,150,450,150);
    line(150,250,450,250);
    line(250,50,250,350);
    line(350,50,350,350);
    line(150,151,450,151);
    line(150,251,450,251);
    line(251,50,251,350);
    line(351,50,351,350);
    ticlogic();

    }






















    void select1()
    {
    settextstyle (8,HORIZ_DIR,1);
    if(y==355)
    {
    setcolor(BLUE);
    for (i=0;i<640;i++)
    { line (i,0,i,15);}
    setcolor(GREEN);
    outtextxy(1,1, " The program is exiting from the console mode please wait...... ");
    delay(3000);
    for(int lines=17;lines<500;lines++)
    {
    setcolor(BLACK);
    line(0,lines,800,lines);
    delay(3);
    }

    closegraph();
    exit(0);
    }
    if(y==320)
    {
    setcolor(8);
    rectangle(635,520,430,170);
    rectangle(634,519,429,169);
    setcolor(WHITE);
    settextstyle (6,HORIZ_DIR,1);
    outtextxy(530,185, "Credits");
    for(int ef=450;ef>200;ef--)

    {
    setcolor(9);
    settextstyle (6,HORIZ_DIR,1);
    outtextxy(530,ef, "Shefiu - The Leader");
    delay(15);
    setcolor(BLACK);
    outtextxy(530,ef, "Shefiu - The Leader");
    }
    delay(20);
    for(ef=450;ef>200;ef--)

    {
    setcolor(9);
    settextstyle (6,HORIZ_DIR,1);
    outtextxy(530,ef, "Vasanth - The Mind");
    delay(15);
    setcolor(BLACK);
    outtextxy(530,ef, "Vasanth - The Mind");
    }
    delay(20);
    for(ef=450;ef>200;ef--)

    {
    setcolor(9);
    settextstyle (6,HORIZ_DIR,1);
    outtextxy(530,ef, "Akhil - Reflection");
    delay(15);
    setcolor(BLACK);
    outtextxy(530,ef, "Akhil - Reflection");
    }
    delay(20);
    for(ef=450;ef>200;ef--)

    {
    setcolor(9);
    settextstyle (6,HORIZ_DIR,1);
    outtextxy(530,ef, "Devo - Assimilator");
    delay(15);
    setcolor(BLACK);
    outtextxy(530,ef, "Devo - Assimilator");
    }
    delay(20);
    for(ef=450;ef>200;ef--)

    {
    setcolor(9);
    settextstyle (6,HORIZ_DIR,1);
    outtextxy(530,ef, "Ridwaan - The Boss");
    delay(15);
    setcolor(BLACK);
    outtextxy(530,ef, "Ridwaan - The Boss");
    }
    delay(20);
    for(ef=450;ef>200;ef--)

    {
    setcolor(9);
    settextstyle (6,HORIZ_DIR,1);
    outtextxy(530,ef, "MH - Listening");
    delay(15);
    setcolor(BLACK);
    outtextxy(530,ef, "MH - Listening");
    }
    delay(20);
    for(ef=450;ef>230;ef--)

    {
    setcolor(9);
    settextstyle (6,HORIZ_DIR,4);
    outtextxy(530,ef-10, "....Group X");
    delay(15);
    setcolor(BLACK);
    outtextxy(530,ef-10, "....Group X");
    }


    delay(1000);
    setcolor(9);
    settextstyle (6,HORIZ_DIR,1);
    outtextxy(530,ef-10, "Shefiu - The Leader");
    outtextxy(530,ef+20, "Vasanth - The Mind");
    outtextxy(530,ef+50, "Akhil - Reflection");
    outtextxy(530,ef+80, "Devo - Assimilator");
    outtextxy(530,ef+110, "Ridwaan - The Boss");
    outtextxy(530,ef+140, "MH - Listening");
    outtextxy(530,ef+170, "Form....");
    settextstyle (6,HORIZ_DIR,4);
    outtextxy(530,ef+210, " ...The Group X");
    delay(7000);
    start();
    }

    if(y==180)
    { setcolor(BLUE);
    for (i=0;i<640;i++)
    { line (i,0,i,15);

    }

    setcolor(GREEN);
    outtextxy(1,1," Tic Tac Toe Game is loading please wait...................");
    delay(500);
    tictactoe();
    start();

    }

    }



    void point()
    {
    int x;
    int x1,y1;
    x1=0;
    y1=0;
    x=400;
    prog:
    setcolor(BLACK);
    for(int ci=0;ci<10;ci++)
    {
    circle(x1,y1,ci);
    }

    setcolor(GREEN);
    for(ci=0;ci<10;ci++)
    {
    setcolor(ci);

    circle(x,y,ci);
    delay(20);
    }

    x1=x;
    y1=y;
    k=getche();
    if(k==0x50)
    y=y+35;
    if(y>355)
    y=180;
    if(k==27)
    exit(0);
    if(k==0x48)
    y=y-35;
    if(y<180)
    y=355;
    if(k==13)
    select1();
    goto prog;

    }

    void main()
    {
    inig();
    start();
    point();
    tictactoe();
    }



    /* Please use code tags next time, just posting it makes it next to unreadable. You could also just attach the file. hth - nv, mod. */

  7. #22
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,596
    Instead of using all that code to fake the animation, use a double buffer and blit the image. I can do it in assembly in about 10 lines, and in C in about 8. For good animation you have to use a double buffer or flip the pointers. You never want the image to disappear from the screen, or the user will see it as a brief flash or flicker since the image does disappear for a very brief amount of time. However, the human eye will catch this flicker.

    One good result/trick of the AND operation is this.

    number MOD (power of 2)

    is the same as

    number AND ((power of 2)-1).

    so x mod 256

    is the same as

    x AND 255

    x AND 255 is much much faster than x mod 256


    It would be interesting to see if the compilers would catch this in an optimization loop. Use the slower method and then look at the asm that the compiler generates.

    Another technique that may or may not help you is getting access to 32-bit opcodes and 32-bit addresses in 16-bit segments.

    either do this:

    asm
    {
    db 66h
    stosw
    }

    or you can use emit() to emit bytes directly into the code stream.
    Personally, I would use a separate asm file.


    Since stosw is preceeded by the 32-bit opcode prefix 66h, it will actually execute stosd.

    The 32-bit address prefix is 67h.


    Most compilers should produce asm from your C code that already takes advantage of these tricks and more. There are many many tricks to speed up pipelined processors. For more information look on a search engine. I have a URL for this, but it is all in assembly, or most of it, and would probably only serve to confuse you.

    One good rule of thumb in any code is don't jump around a lot. Every time you call another function, a new stack frame is created and if that function has parameters, a lot of pushing and popping is going on. This is true for all calls. The more you jump around, the slower your code will execute even with jump optimization on. The compiler will attempt to optimize your jumps but it is possible to structure your code in such a way as to defeat this optimization. So in tight loops that need to be fast, make sure you do not jump or call out of that loop or you will slow it down.

    Also for tight loops and fast code you should make use of the register prefix on variables. Now there are a limited number of registers, but this prefix will tell the compiler that you prefer your variables to be stored in registers. This is optional to the compiler and can only be done if there are indeed any registers that are free to be used. However in small loops with limited variables this can speed up your code somewhat. You can turn automatic register variables on in your compiler options as well. The default scheme then would be for your compiler to attempt to store as many variables as it can in registers instead of memory.

    To really understand how to make your code faster, in my opinion, I think you would need to understand a bit of assembly. Many might disagree on this point, but my knowledge of assembly has helped a lot.

    And, of course, make sure that you use a profiler. Often times the problem areas might not be exactly where the programmer 'thinks' they are. The profiler will give you a much better picture of what's going on when and where.

    My last optimization is with arrays. Lots of people use 2D arrays. I've read time and time again that these access slower than 1D.

    To access a 1D array, but treat it like a 2D you would do

    row*width+column for row major ordering. There is also column major ordering but same idea. Also if your array is a power of 2, and it should be, then you can use bitshifts to eliminate the mul inherent in the formula.

    But I could go on and on about tricks and optimizations. Some of them are mute points on todays processors and compilers and some are not. Like I said look this up on www.google.com and you can find out a lot more information. But, in your code, try to work with the processor instead of against it and your code will get along much better with the CPU.

  8. #23
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >is that so? why weren't we informed of this notation!!! when is
    >that ever true? that's pretty far out...
    Array notation in C and C++ is associative. The notation a[i] will be degraded into *( a + i ), which we all know can be reversed to *( i + a ) with the same result. Hence, a[i] can also be written as i[a] and work just the same:
    Code:
    #include <stdio.h>
    
    int main ( void )
    {
      char a[] = "Julienne";
      printf ( "%c\n", 3[a] );
      return 0;
    }
    Here's another trick I see too often, it magically makes your program completely undefined with one line of code:
    void main ( void )

    -Prelude
    My best code is written with the delete key.

  9. #24
    I'm Back
    Join Date
    Dec 2001
    Posts
    556
    vasanth - is that all??? just check if something isn't missing in the program.
    [/sarcasm]


    AHHA so Prelude your real name is Julienne.

  10. #25
    Registered User
    Join Date
    Apr 2002
    Posts
    142
    bubba, thanks, that's quite an effort you posted up here,

    prelude, it's commutative,

    unregisterd, what do you mean? i want to build this website myself,

    thanks all but i hope to get more tricks so that later on i can have make up sections for the website,

  11. #26
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >prelude, it's commutative
    Oops. Let this be a lesson to you, don't answer technical questions when you're only half awake

    -Prelude
    My best code is written with the delete key.

  12. #27
    Its not rocket science vasanth's Avatar
    Join Date
    Jan 2002
    Posts
    1,683
    Any way i am new to programming...
    How is the graphics anyway...

  13. #28
    Registered User
    Join Date
    Apr 2002
    Posts
    142
    hey guys,

    i've finally put up the website, i hope i could get some contributions here,

    http://www.angrysoft.com/tipsandtricks.php
    http://www.angrysoft.com/
    Get SMS Counter and Call Counter for Nokia 7650/3650

    http://www.angrysoft.com/tipsandtricks.php
    Please contribute some tips and tricks that you know of.

  14. #29
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    here's an... um... trick? idk... it amazes amatuer programmers when they get stuck with accumulators and if statements...

    Code:
    //instead of:
    if(a==b)
       a=5
    else
       a=b
    //do this:
    a=(a==b?5:b);
    and:
    Code:
    //instead of:
    this_variable=this_variable+1;
    //try
    this_variable++;
    //or, instead of: 
    this_variable=this_variable+2;
    //do this:
    this_variable+=2;
    it's the simple things in life...
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  15. #30
    Un Artiste Extraordinaire volk's Avatar
    Join Date
    Dec 2002
    Posts
    357
    Those tricks are amazing.

Page 2 of 4 FirstFirst 1234 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Beginning Game Programming Type Books
    By bumfluff in forum Game Programming
    Replies: 36
    Last Post: 09-13-2006, 04:15 PM
  2. C programming - pls pls help me
    By sally arnold in forum C Programming
    Replies: 10
    Last Post: 01-16-2002, 03:55 AM
  3. pls help me!!
    By hanseler in forum C++ Programming
    Replies: 1
    Last Post: 12-05-2001, 07:46 PM
  4. Pls Help Me In This Question!!!
    By Joanna in forum Windows Programming
    Replies: 1
    Last Post: 10-20-2001, 02:05 PM
  5. 40 post drop
    By gamegod3001 in forum A Brief History of Cprogramming.com
    Replies: 6
    Last Post: 10-12-2001, 12:14 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21