Hello,
I was trying to make a little pong demo, just for fun. Here is the code:
File Pong.gbas
LIMITFPS 60
GLOBAL mx%, my%, ma%, mb%
GLOBAL screenW%, screenH%
GETSCREENSIZE screenW%, screenH%
LOCAL player AS TPaddle
LOCAL opponent AS TPaddle
LOCAL centerPaddle% = (screenH%/2) - (paddleHeight% / 2)
player.init(24, centerPaddle%, FALSE, FALSE)
opponent.init(screenW-24-paddleWidth%, centerPaddle%, FALSE, TRUE)
//////////////
// main loop
WHILE TRUE
CLEARSCREEN
player.update()
player.render()
opponent.update()
opponent.render()
renderFPS()
SHOWSCREEN
WEND
//////////////
// render FPS
FUNCTION renderFPS:
GLOBAL dtime%
GLOBAL fps%
dtime = GETTIMER()
fps = ((1000/dtime)+fps)/2
PRINT "FPS: " + FORMAT$(3, 0, fps), 0, 0
ENDFUNCTION
File Types.gbas
GLOBAL mx%, my%, ma%, mb%
GLOBAL screenW%, screenH%
CONSTANT verticalLimit% = 20
CONSTANT paddleWidth% = 40
CONSTANT paddleHeight% = 100
CONSTANT mouseSensibility = 1.5
TYPE TPaddle
x%
y%
isComputer%
isOpponent%
FUNCTION init: x%, y%, isComputer%, isOpponent%
self.x% = x%
self.y% = y%
self.isComputer% = isComputer%
self.isOpponent% = isOpponent%
ENDFUNCTION
FUNCTION render:
DRAWRECT self.x%, self.y%, paddleWidth%, paddleHeight%, RGB(255,255,255)
ENDFUNCTION
FUNCTION update:
IF NOT self.isComputer%
MOUSESTATE mx%, my%, ma%, mb%
IF NOT self.isOpponent%
self.y% = self.y% + MOUSEAXIS(1) * mouseSensibility
ELSE
self.y% = self.y% - MOUSEAXIS(1) * mouseSensibility
ENDIF
IF self.y% > screenH% - paddleHeight% - verticalLimit%
self.y% = screenH% - paddleHeight%- verticalLimit%
ENDIF
IF self.y% < verticalLimit%
self.y% = verticalLimit%
ENDIF
SETMOUSE mx%, self.y%
ENDIF
ENDFUNCTION
ENDTYPE
The two paddles are controlled by mouse. The weird thing is this: when I try to move the mouse back and forth, the paddles move accordingly to it, but after a while, the movements start to limp. Sometimes I run the demo from the IDE, source unmodified, and the paddles don't move at all! Where is the problem??
How long does it take before the mouse starts to become erratic? I just tried for a while and had no obvious problems (running from the IDE).
Quote from: Ian Price on 2010-Aug-29
How long does it take before the mouse starts to become erratic? I just tried for a while and had no obvious problems (running from the IDE).
Almost immediately. It begins smoothly, and little by little, it ends to limp in a couple of seconds...
QuoteAlmost immediately. It begins smoothly, and little by little, it ends to limp in a couple of seconds...
Not here is didn't. I tried for at least 30 seconds - both with my mouse and trackpad.
Nope. Tried again. Mouse control seemed pretty stable.
I forgot to say that I'm using 8.085 ver. I really don't know, maybe my machine is faulty (or Vista perhaps). I will try to reboot my notebook, and we will see... Thanks for your help
I'm using v8.085 too on Win7 64bit laptop. The clue might be in the "notebook" bit. Dunno.
Fine here too. Occasionally, I have noticed that a program will run, but not have input focus - its possible you occasionally need to click anywhere in the running program to regain focus and hopefully solve the problem.
I have modified the code this way:
Pong.gbas
LIMITFPS 60
GLOBAL mx%, my%, ma%, mb%
GLOBAL screenW%, screenH%
GETSCREENSIZE screenW%, screenH%
LOCAL player AS TPaddle
LOCAL opponent AS TPaddle
LOCAL centerPaddle% = (screenH%/2) - (paddleHeight% / 2)
player.init(24, centerPaddle%, FALSE, FALSE)
opponent.init(screenW-24-paddleWidth%, centerPaddle%, FALSE, TRUE)
//////////////
// main loop
WHILE TRUE
CLEARSCREEN
MOUSESTATE mx%, my%, ma%, mb%
LOCAL mouseMove% = MOUSEAXIS(1)
player.update(mouseMove%)
player.render()
opponent.update(mouseMove%)
opponent.render()
renderFPS()
SHOWSCREEN
WEND
//////////////
// render FPS
FUNCTION renderFPS:
GLOBAL dtime%
GLOBAL fps%
dtime = GETTIMER()
fps = ((1000/dtime)+fps)/2
PRINT "FPS: " + FORMAT$(3, 0, fps), 0, 0
ENDFUNCTION
Types.gbas
GLOBAL screenW%, screenH%
CONSTANT verticalLimit% = 20
CONSTANT paddleWidth% = 40
CONSTANT paddleHeight% = 100
CONSTANT mouseSensibility = 1.5
TYPE TPaddle
x%
y%
isComputer%
isOpponent%
FUNCTION init: x%, y%, isComputer%, isOpponent%
self.x% = x%
self.y% = y%
self.isComputer% = isComputer%
self.isOpponent% = isOpponent%
ENDFUNCTION
FUNCTION render:
DRAWRECT self.x%, self.y%, paddleWidth%, paddleHeight%, RGB(255,255,255)
ENDFUNCTION
FUNCTION update: mouseMove%
IF NOT self.isComputer%
IF NOT self.isOpponent%
self.y% = self.y% + mouseMove% * mouseSensibility
ELSE
self.y% = self.y% - mouseMove% * mouseSensibility
ENDIF
IF self.y% > screenH% - paddleHeight% - verticalLimit%
self.y% = screenH% - paddleHeight% - verticalLimit%
ENDIF
IF self.y% < verticalLimit%
self.y% = verticalLimit%
ENDIF
ENDIF
ENDFUNCTION
ENDTYPE
and I solved the problem. Maybe calling the functions MOUSESTATE or MOUSEAXIS more than once before SHOWSCREEN screws the mouse input... :|
P.S.: I have an Acer Aspire 5520G-302G16, I never really liked Acer's laptops :P
Yes - that would happen because SDL doesn't buffer mouse movement...
Quote from: MrTAToad on 2010-Aug-31
Yes - that would happen because SDL doesn't buffer mouse movement...
I don't knew GLBasic relies on SDL libraries, now I see. Thanks for the help :good:
No problem!