Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » +20hours on just Camera

Credits go to beoran, Dizzy Egg, and Mark Oates for helping out!
This thread is locked; no one can reply to it. rss feed Print
+20hours on just Camera
trieveonc
Member #15,638
June 2014

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 ;D.

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.

beoran
Member #12,636
March 2011

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.

Dizzy Egg
Member #10,824
March 2009
avatar

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.

:-*

----------------------------------------------------
Please check out my songs:
https://soundcloud.com/dont-rob-the-machina

trieveonc
Member #15,638
June 2014

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

Dizzy Egg
Member #10,824
March 2009
avatar

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

----------------------------------------------------
Please check out my songs:
https://soundcloud.com/dont-rob-the-machina

trieveonc
Member #15,638
June 2014

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

Dizzy Egg
Member #10,824
March 2009
avatar

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....

----------------------------------------------------
Please check out my songs:
https://soundcloud.com/dont-rob-the-machina

trieveonc
Member #15,638
June 2014

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

Mark Oates
Member #1,146
March 2001
avatar

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.

--
Visit CLUBCATT.com for cat shirts, cat mugs, puzzles, art and more <-- coupon code ALLEGRO4LIFE at checkout and get $3 off any order of 3 or more items!

AllegroFlareAllegroFlare DocsAllegroFlare GitHub

trieveonc
Member #15,638
June 2014

**the relevant parts of the code***I included the code same document in both attachments*

//Structs in .h

#SelectExpand
1struct SpaceShip { 2 int ID; 3 int x; 4 int y; 5 int lives; 6 int speed; 7 int boundx; 8 int boundy; 9 int score; 10 11 int maxFrame; 12 int curFrame; 13 int frameCount; 14 int frameDelay; 15 int frameWidth; 16 int frameHeight; 17 int animationColumns; 18 int animationDirection; 19 20 int animationRow; 21 22 ALLEGRO_BITMAP *image; 23struct Camera 24{ 25 26 27int width; 28int height; 29float x; 30float y; 31float *cameraPosition; 32}; 33 34}; 35//in MAIN.cpp

//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

#SelectExpand
1void DrawShip(SpaceShip &ship,float *cameraPosition,Camera &camera) 2{ 3 int fx =(ship.curFrame % ship.animationColumns) * ship.frameWidth; 4 int fy = ship.animationRow * ship.frameHeight; 5 6 al_draw_bitmap_region(ship.image, fx, fy, ship.frameWidth, 7 ship.frameHeight, ship.x-cameraPosition[0],ship.y-cameraPosition[1], 0); 8 9void InitCamera(Camera &camera,float *cameraPosition, SpaceShip &ship) 10{ 11 int x=0; 12 int y=0; 13cameraPosition[0] = -(WIDTH / 2) + ( x+30 / 2); 14cameraPosition[1] = -(HEIGHT / 2) + ( y+30 / 2); 15 16if(cameraPosition[0] < 0) 17cameraPosition[0] = 0; 18if(cameraPosition[1] < 0) 19cameraPosition[1] = 0; 20if(cameraPosition[0] > (mapwidth * mapblockwidth) - WIDTH) 21cameraPosition[0] = (mapwidth * mapblockwidth) - HEIGHT; 22if(cameraPosition[1] > (mapheight * mapblockheight) - WIDTH) 23cameraPosition[1] = (mapheight * mapblockheight) - HEIGHT; 24 25} 26void ChangeState(int &state, int newState) 27{ 28 29 state = newState; 30 31 if(state == TITLE) 32 { 33 } 34 else if(state == PLAYING) 35 { 36 InitCamera(camera)//saying undefined 37 InitShip(ship); 38 39 al_play_sample_instance(songInstance); 40 } 41 else if(state == LOST) 42 { 43 } 44}

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

Mark Oates
Member #1,146
March 2001
avatar

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:

608714

--
Visit CLUBCATT.com for cat shirts, cat mugs, puzzles, art and more <-- coupon code ALLEGRO4LIFE at checkout and get $3 off any order of 3 or more items!

AllegroFlareAllegroFlare DocsAllegroFlare GitHub

trieveonc
Member #15,638
June 2014

AFTER 25 HOURS I DID IT!!! thank you all...

Mark Oates
Member #1,146
March 2001
avatar

I'm curious, what was the "thing" that you didn't get until the end?

--
Visit CLUBCATT.com for cat shirts, cat mugs, puzzles, art and more <-- coupon code ALLEGRO4LIFE at checkout and get $3 off any order of 3 or more items!

AllegroFlareAllegroFlare DocsAllegroFlare GitHub

trieveonc
Member #15,638
June 2014

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.

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

trieveonc said:

#SelectExpand
1draw ship() 2{ 3cameraPosition[0] = -(WIDTH / 2) + (x + width / 2); 4cameraPosition[1] = -(HEIGHT / 2) + (y + height / 2); 5 6if(cameraPosition[0] < 0) 7cameraPosition[0] = 0; 8if(cameraPosition[1] < 0) 9cameraPosition[1] = 0; 10if(cameraPosition[0] > (mapwidth * mapblockwidth) - WIDTH)
11cameraPosition[0] = (mapwidth * mapblockwidth) - HEIGHT;
12if(cameraPosition[1] > (mapheight * mapblockheight) - WIDTH)
13cameraPosition[1] = (mapheight * mapblockheight) - HEIGHT; 14 15int fx =(ship.curFrame % ship.animationColumns) * ship.frameWidth; 16int fy = ship.animationRow * ship.frameHeight; 17 18al_draw_bitmap_region(ship.image, fx, fy, ship.frameWidth, 19ship.frameHeight, x-cameraPosition[0],y-cameraPosition[1], 0);

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.

trieveonc
Member #15,638
June 2014

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.

Go to: