visual c++ - Having trouble moving a reticle around the screen while following Chili's Beginning DirectX tutorial -
to begin using chili framework lessons 1-15 downloadable here: http://www.planetchili.net/
i using directx 9 on old laptop running windows xp sp3. have set direct3d rendering software in order run framework. using visual studio express c++ 2010 first service pack installed.
this code having trouble with:
// start moving reticle code drawreticle(itemlocx, itemlocy, 255, 255, 255); if(itemlocx == pointa && itemlocx != pointab) { itemlocx += 2; } else if(itemlocx == pointbc && itemlocx != pointda) { itemlocx -= 2; } if(itemlocy == pointab && itemlocy != pointbc) { itemlocy += 2; } else if(itemlocy == pointda && itemlocx != pointa) { itemlocy -= 2; } // end moving reticle code
now chili's solution move along y axis while checking x, , x while checking y. may post later, don't have readily available. can see @ beginning of video: http://youtu.be/jemwkqsi8l0
however wanted logically, if walking border along invisible wall inside box. wanted make sense going on. cursor won't move, , see no reason why doesn't. here game.h:
#pragma once #include "d3dgraphics.h" #include "keyboard.h" class game { public: game( hwnd hwnd,const keyboardserver& kserver ); void go(); private: void composeframe(); /********************************/ /* user functions */ void drawreticle(int xp, int yp, int cr, int cg, int cb); /* xp = x position, yp = y position, cr = color red, cg = color green, cb = color blue */ // todo: user functions go here /********************************/ private: d3dgraphics gfx; keyboardclient kbd; /********************************/ /* user variables */ int pointa; // starting @ pointa (100, 100) - top left int pointab; // move pointa pointab (700, 100) - top right int pointbc; // move pointab pointbc (700, 500) - bottom right int pointcd; // move pointbc pointcd (100,500) - bottom left int pointda; // move pointcd pointda (100,100) - top left /* these points describe process of starting, 4 movements. 4 points a, b, c, d. start @ a, go b (pointab, read b), go c (pointbc, read b c), go d (pointcd, read c d) go (pointda, read d a). can confusing, because there 5 varibles used. if drew out there 4 points, 4 movements. best way think of starting movement, , need place start from, must have point. since start @ a, haven't yet gone anywhere, pointa our starting point. once start moving, go pointa pointb. if used pointb our variable confusing,because have move pointa pointb pointc pointd , pointa. still 5 variables, 1 repeating, first pointa describes start, , last end. since these 2 different actions on same point, have elected use 2 letter names each of points move to, while point start @ has single letter name. best way think process. */ int itemlocx; // initial position of item on x axis int itemlocy; // initial position of item on y axis int reticlex; // initial position of reticle on x axis int reticley; // initial position of reticle on y axis // todo: user variables go here /********************************/ };
here game.cpp:
#include "game.h" game::game( hwnd hwnd,const keyboardserver& kserver ) : gfx(hwnd), kbd(kserver), itemlocx(100), // initial position of item on x axis itemlocy(100), // initial position of item on y axis reticlex(400), // initial position of reticle on x axis reticley(300), // initial position of reticle on y axis pointa(100), // movement 0 a, stopping @ pointab(700), // movement b, stopping @ b pointbc(500), // movement b c, stopping @ c pointcd(700), // movement c d, stopping @ d pointda(500) // movement d a, stopping @ {} void game::go() { gfx.beginframe(); composeframe(); gfx.endframe(); } void game::drawreticle(int xp, int yp, int cr, int cg, int cb) /* xp = x position, yp = y position, cr = color red, cg = color green, cb = color blue */ { gfx.putpixel(xp-5,yp,cr,cg,cb); gfx.putpixel(xp-4,yp,cr,cg,cb); gfx.putpixel(xp-3,yp,cr,cg,cb); gfx.putpixel(xp+3,yp,cr,cg,cb); gfx.putpixel(xp+4,yp,cr,cg,cb); gfx.putpixel(xp+5,yp,cr,cg,cb); gfx.putpixel(xp,yp,cr,cg,cb); gfx.putpixel(xp,yp-5,cr,cg,cb); gfx.putpixel(xp,yp-4,cr,cg,cb); gfx.putpixel(xp,yp-3,cr,cg,cb); gfx.putpixel(xp,yp+3,cr,cg,cb); gfx.putpixel(xp,yp+4,cr,cg,cb); gfx.putpixel(xp,yp+5,cr,cg,cb); } void game::composeframe() { // start draw reticle code drawreticle(reticlex, reticley, 100, 155, 255); // end draw reticle code // start color change code int yt = 200; // border 200 pixels top int yb = 400; // border 200 pixels bottom int xl = 300; // border 200 pixels left int xr = 500; // border 200 pixels right if(reticlex < xl || reticlex > xr) // defining color change area x { drawreticle(reticlex, reticley, 255, 255, 255); } if(reticley < yt || reticley > yb) // defining color change area y { drawreticle(reticlex, reticley, 255, 255, 255); } // end color change code // start moving reticle code drawreticle(itemlocx, itemlocy, 255, 255, 255); if(itemlocx == pointa && itemlocx != pointab) { itemlocx += 2; } else if(itemlocx == pointbc && itemlocx != pointda) { itemlocx -= 2; } if(itemlocy == pointab && itemlocy != pointbc) { itemlocy += 2; } else if(itemlocy == pointda && itemlocx != pointa) { itemlocy -= 2; } // end moving reticle code // start border code if(reticlex < 6) { reticlex = 6; } else if(reticlex > 794) { reticlex = 794; } if(reticley < 6) { reticley = 6; } else if(reticley > 594) { reticley = 594; } // end border code // start speed change code int cspeed = 4; // default cursor speed if(kbd.enterispressed()) // change high speed { cspeed = 8; } if(kbd.spaceispressed()) // change low speed { cspeed = 1; } if(kbd.rightispressed()) { reticlex += cspeed; } if(kbd.leftispressed()) { reticlex -= cspeed; } if(kbd.upispressed()) { reticley -= cspeed; } if(kbd.downispressed()) { reticley += cspeed; } // end speed change code }
now should note here should done without functions , basic c++ operators. that's far chili has taught point. second attempt solve myself, after hours thinking , working on on paper. i'm stuck. not seeing it. think there logic error here on part. want understand thinking may mistaken, more that, how think correctly, computer, this.
i open advice regarding coding style. if not being clear enough, or doing should not become bad habit - if there should doing differently in writing code know it.
thank - appreciated!
i see how have tried this. have on complexed it.
1: don't need != operator in if statements.
2: try this:
if(itemlocx < 700) { itemlocx += 2; }
3: worked fine during testing. point if statements in wrong order. changed order in moved across screen in. have x y x y , have x x y y. (unconfirmed) executes if statements in order. have hard coded answer. set them variables if want to. hope helped!
Comments
Post a Comment