and Fail still
I even was using xOff and yOff But its not really as effective as adding a camera
im willing to pay//also i have a $10bucks amazon card (i know its not much) if someone could just please go in my code and add a camera thats it!I would deeply and sincerely appreciate it.
i tried over countless methods tyring to implement it. If someone could do it for me and ill learn from it I know its not typical but me about to shoot myself .
Its very complicated adding a camera when your using function and game states.
InitShip Player
P.S
I think I posted this in wrong forum before, i know how to add a basic camera its jut not meshing with my code.
It may be a bit overkill but my RPG engine has a fully featured camera module, here:
https://github.com/beoran/eruta/blob/master/src/camera.c.
I know it can be frustrating to work a long time without finding an answer, but it's important not to just copy what others are doing.
As for a camera system, think of it like this: a camera can be represented by a struct that contains the x, y, width and height values of the camera. For the x
camera to actually work, everything that is drawn should be drawn at it's own "world" x and y coordinates MINUS the x and y coordinates of the camera.
trueveonc, listen, you can't just say "add a camera".
I looked through your code, and it's too much.
Tell us how it works, what doesn't work, take snippets of code that don't work AND TELL US WHY THEY DON'T WORK and what you expect them to do.
You can't just say "add a camera".
Too arbitrary, too vague.
thank you for your replies.
For the Camera
draw ship()
{
cameraPosition[0] = -(WIDTH / 2) + (x + width / 2);
cameraPosition[1] = -(HEIGHT / 2) + (y + height / 2);
if(cameraPosition[0] < 0)
cameraPosition[0] = 0;
if(cameraPosition[1] < 0)
cameraPosition[1] = 0;
if(cameraPosition[0] > (mapwidth * mapblockwidth) - WIDTH)
cameraPosition[0] = (mapwidth * mapblockwidth) - HEIGHT;
if(cameraPosition[1] > (mapheight * mapblockheight) - WIDTH)
cameraPosition[1] = (mapheight * mapblockheight) - HEIGHT;
int fx =(ship.curFrame % ship.animationColumns) * ship.frameWidth;
int fy = ship.animationRow * ship.frameHeight;
al_draw_bitmap_region(ship.image, fx, fy, ship.frameWidth,
ship.frameHeight, x-cameraPosition[0],y-cameraPosition[1], 0);
and its intiated in main as so....
if(redraw && al_is_event_queue_empty(event_queue))
{
redraw = false;
if(state == TITLE)
{
al_draw_bitmap(title, 0, 0, 0);
}
else if(state == PLAYING)
{
MapDrawBG(0,0, 0, 0, 3200, 3200);
DrawSprites(sprite);
DrawShip(ship);
DrawBullet(bullets, NUM_BULLETS);
DrawComet(comets, NUM_COMETS);
DrawExplosions(explosions, NUM_EXPLOSIONS);
ive also tried doing it as a voidUpdateCamera prototype instead of just pasting it in my player struct.
I used transform camera and update camera as such i ran into the problem since my player was in a struct and was not in main....
i couldnt insert cameraposition in al_draw_bitmap_region(player, and camera was undefined because im using a struct for my player.
I can play with it and get no error but my camera just doesnt want to intialize
P.S**this maybe easier*
can I use xOff and Yoff instead of a camera?
when i run the program i can move through the map its just i cant go to the edges
for example..... the 1's if this was a tile map. I can only move through the 0s is there a way to fix to this to avoid the headache of the camera
111111
100001
100001
111111
xOff += keys[RIGHT] *5;
xOff -= keys[LEFT] *5;
yOff += keys[DOWN] *5;
yOff -= keys[UP] * 5;
if(xOff < 0)
xOff = 0;
if(yOff < 0)
yOff = 0;
if(xOff > (mapwidth*mapblockwidth - WIDTH))
xOff = mapwidth*mapblockwidth - WIDTH;
if(yOff > mapheight*mapblockheight - HEIGHT)
yOff = mapheight*mapblockheight - HEIGHT;
MapDrawBG(xOff,yOff, 0, 0,WIDTH, HEIGHT);
NO ERROR OR WARNINGS BTW.
sorry for being a newb
I don't understand what you mean....you're saying that if camera.x, or xOff goes below 0, make it 0, and it it goes above the screen width etc, make it the screen width...
...what exactly are you trying to do? Are you saying that this is your map:
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
...but you can only move like this (you're trapped in the 1's, which are SCREEN_W and SCREEN_H)??:
1111100000000000
1000100000000000
1000100000000000
1111100000000000
0000000000000000
0000000000000000
Sorry for being confusing.
i can move like this(using mappy btw using 1s and 0s as examples)
if this was my entire map
111111111
111111111
100000011
100000011
100000011
111111111
111111111
only can move in 0sits like i get a certain distance from the side and it doesnt let me keep going and its equal in all directions.
this is using xOff and yOff
no camera.
If i cant use this method ill just delete it from my program if I can wud be great..also to mention my map is big so when im moving through the 0s the screen is scrolling until I to those 1s in which i cant access the rest of map.
I have a quick question since you went through my code.
U saw i was using game states and a player struct where do include the camera in my player struct or as a prototype
Ok, so let's assume you're using xOff and yOff....show me the code where you use xOff and yOff to affect your tiles....(ie, show me how you use xOff and yOff when drawing)...
[edit]
You store your camera wherever you want to! Personally I would have:
Player
Camera
Map
Tiles
...etc etc, and then have functions like....
funcDrawPlayer()
funcDrawMap(&Camera)
...etc etc....
edit sending the actual code shortly.
the code above is all the xOff Yoff in my program i just put it in before it draws my map so it sets map boundaries,it also allows me to move through the map
and yIm just gunna scrap this xOff yOff stuff
ima do the the way you mentioned
in objects.h i have
//struct
struct Camera
{
float *cameraPosition; this doing nothing i thnk
int width;
int height;
float x;
float y;
};
in main.cpp
//prototypes
initShip(blah)//this is player
drawShi()
InitCamera(Camera &camera,float *cameraPosition)
//main
int main()
float position[2]
float cameraPosition[2]={0,0};
allegro_transform *camera
return 0;
//below main
InitCamera(Camera &camera,float *cameraPosition)
cameraPosition[0] = -(WIDTH / 2) + (x + 30 / 2);
cameraPosition[1] = -(HEIGHT / 2) + (y + 30 / 2);
if(cameraPosition[0] < 0)
cameraPosition[0] = 0;
if(cameraPosition[1] < 0)
cameraPosition[1] = 0;
if(cameraPosition[0] > (mapwidth * mapblockwidth) - WIDTH)
cameraPosition[0] = (mapwidth * mapblockwidth) - HEIGHT;
if(cameraPosition[1] > (mapheight * mapblockheight) - WIDTH)
cameraPosition[1] = (mapheight * mapblockheight) - HEIGHT;
}
DrawSpaceship(Ship &ship,Camera &Camera,float *cameraPosition)
int fx =(ship.curFrame % ship.animationColumns) * ship.frameWidth;
int fy = ship.animationRow * ship.frameHeight;
al_draw_bitmap_region(ship.image, fx, fy, ship.frameWidth,
ship.frameHeight, x-cameraposition[0],y-cameraposition[0], 0);
//i put in game state playing
if state playing...
InitCamera(cameraPosition,x,y,32,32);
NO Errors or Warnings
If you use an ALLEGRO_TRANSFORM, you have the option to no longer need offset_xs or offset_ys littered throughout your sprite drawing routines.
**the relevant parts of the code***I included the code same document in both attachments*
//Structs in .h
//prototypes
void InitShip(SpaceShip &ship, ALLEGRO_BITMAP *image); void InitCamera(Camera &camera,float *cameraPosition, SpaceShip &ship); void DrawShip(SpaceShip &ship,Camera &camera);
//int main()
int main() Camera camera; float cameraPosition[2]={0,0}; InitCamera(camera,cameraPosition,ship); else if(state == PLAYING) MapDrawBG(0,0, 0, 0,WIDTH, HEIGHT); DrawShip(ship,camera); return 0;
//protype below main
saying camera is undefined when i Init it
i feel like im in the right direction with it stuck on 23 hours working with stupid camera. im online if anyone wants to walk me through it
idk why it isnt following me
Well, when posting, please put your code inside <code></code> tags.
Also, you can edit your previous posts to make those changes, by clicking that icon:
AFTER 25 HOURS I DID IT!!! thank you all...
I'm curious, what was the "thing" that you didn't get until the end?
I was used to setting up a camera for small programs.
void UpdateCamera prototype
then everything else in main without calling the player function
whereas
I used functions to get the variables i wanted like the player variables and correlated them with my camera function.
*if that makes any sense*
Just got done with one c++class and wanted to try programming a RPG been a goal of mine since i was a kid.
But im a newb atm so i greatly appreciate the collective information from the allegro community.
thanks again.
You've swapped WIDTH and HEIGHT there on the two highlighted lines.
It should be :
if(cameraPosition[0] > (mapwidth * mapblockwidth) - WIDTH)cameraPosition[0] = (mapwidth * mapblockwidth) - WIDTH;if(cameraPosition[1] > (mapheight * mapblockheight) - HEIGHT)cameraPosition[1] = (mapheight * mapblockheight) - HEIGHT;
And, you should share your solution in case it would help someone else. I still don't know what you did.
so i implented the basic camera, but its causing my sprites to follow the player on the screen instead of staying at their world coordinates.
whats going on with my camera its moving my screenheight and width along my map instead of a small camera just on the player.
the code
void CameraUpdate(float *cameraPosition, float x, float y, int width, int height,SpaceShip&ship) { cameraPosition[0] = ship.x-WIDTH/2; cameraPosition[1] = ship.y-HEIGHT/2; if(cameraPosition[0] < 0) cameraPosition[0] = 0; if(cameraPosition[1] < 0) cameraPosition[1] = 0; if(cameraPosition[0] > (mapwidth * mapblockwidth) - WIDTH) cameraPosition[0] = (mapwidth * mapblockwidth) - WIDTH; if(cameraPosition[1] > (mapheight * mapblockheight) - HEIGHT) cameraPosition[1] = (mapheight * mapblockheight) - HEIGHT; }
playerr
al_draw_bitmap_region(ship.image, fx, fy, ship.frameWidth, ship.frameHeight, ship.x-cameraPosition[0],ship.y-cameraPosition[1], 0);
the sprite following the screen at 1,1 instead of staying there
void DrawSprites(Sprite &sprite)
{
int fx = (sprite.curFrame % sprite.animationColumns) * sprite.frameWidth;
int fy = (sprite.curFrame / sprite.animationColumns) * sprite.frameHeight;
al_draw_bitmap_region(sprite.image, fx, fy, sprite.frameWidth,
sprite.frameHeight,1,1, 0);
}
basically the problem is the camera is following my screen instead of my player i believe.