Tangent128 / luasdl2

A pure C binding of SDL 2.0 for Lua 5.1, Lua 5.2, and LuaJIT.
ISC License
385 stars 74 forks source link

[QUESTION] working with classes that have images #29

Closed lewislepton closed 8 years ago

lewislepton commented 9 years ago

something that has been getting on my nerves, or at least has been bothering, is working with classes/modules with an image player, but its the render stuff that is very confusing.

whilst i can get a window to be in a seperate module and call it in the main one is fine. its actually the rendering of an image from within a module that is troublesome or more so, confusing to say the least.

its when using createRenderer that is confusing me the most. since that calls in a window too it to render to. but you wouldnt need a window setting for each things would you, that wouldnt mak sense. so how can you get by that?

here is a completely unfinished hodge-podge of code ive been rying to get to work to use an image inside a class to then call up in the main.lua. if anyone can help me just get it working id be happy. sdl/c++ ones have been helpful, but really on putting them to lua-SDL2, its been pretty cack

main.lua

SDL = require 'SDL'
player = require 'player'

local graphics = {}
local pos = {}
local width = 32
local height = 32
local dir = 6

function moveInit()
    assert(SDL.init{SDL.flags.Video})
    -- assert(SDL.joystickOpen(0))
    local win = assert(SDL.createWindow{title = 'moveJoy', width = 640, height = 480})
    -- local rndr = assert(SDL.createRenderer(win, 0, 0))
    -- local image = assert(img.load('player.png'))

    -- local imgTex = assert(rndr:createTextureFromSurface(image))

    -- rndr:setDrawColor(0xFFFFFF)

    -- graphics.win = win
    -- graphics.rndr = rndr
    -- graphics.imgTex = imgTex

    -- ---setting up our image width and height
    -- local f,a,w,h = imgTex:query()
    -- pos.x = width /2 - w / 2
    -- pos.y = height /2 - w / 2
    -- pos.w = w
    -- pos.h = h
end

moveInit()
initPlayer()
local keys = SDL.getKeyboardState()

while true do
    -- graphics.rndr:clear()
    -- graphics.rndr:copy(graphics.imgTex, nil, pos)
    -- graphics.rndr:present()

    SDL.pumpEvents()
    if keys[SDL.scancode.Escape] then
        break
    end

    -- for e in SDL.pollEvent() do
    --  if e.button == 9 then
    --      -- pos.x = pos.x + dir
    --      -- print(string.format("button up: %d", e.button))
    --      break
    --  end
    --  ---axis 0 is the left analog stick but for LEFT/RIGHT
    --  if e.axis == 0 then
    --      ---this will make the image go LEFT on the screen
    --      if e.value < -10 then
    --          pos.x = pos.x - dir
    --          ---this will make the player go RIGHT the screen
    --      elseif e.value > 10 then
    --          pos.x = pos.x + dir
    --      end
    --      ---this just prints our axis 0 [left/right] to know what is happening. good for output test
    --      print(string.format("axis %d: %d", e.axis, e.value))
    --  end
    --  ---axis 1 is the left analog stick but for UP/DOWN
    --  if e.axis == 1 then
    --      ---this will make the image go UP the screen
    --      if e.value < -10 then
    --          pos.y = pos.y - dir
    --      ---this will make the player go DOWN the screen
    --      elseif e.value > 10 then
    --          pos.y = pos.y + dir
    --      end
    --      ---this just prints our axis 1 [up/down] to know what is happening. good for output test
    --      print(string.format("axis %d: %d", e.axis, e.value))
    --  end
    -- end
end

player.lua

local SDLimage = require 'SDL.image'

speed = 6
frameWidth = 32
frameHeight = 32
graphics = {}

function initPlayer()
    assert(SDL.init{SDL.flags.Joystick})
    assert(SDL.joystickOpen(0))

    assert(SDLimage.init{SDL.flags.PNG})
    imageSurface = SDL.Surface

    rndr = assert(SDL.createRenderer())

    image = assert(SDLimage.load('player.png'))
    imgTexture = assert(rndr:createTextureFromSurface(image))

end

-- while true do
--  -- SDL.pumpEvents()
--  -- for e in SDL.pollEvent() do
-- end
Tangent128 commented 9 years ago

If this is still relevant-

You already initialize SDL from the main script; you should not be reinitializing SDL in your modules.

Nor should you be creating renderers, windows, surfaces, etc., from your player module- you want to use the same one you already made, not a new blank one.

So, you should pass the existing objects into your player functions as arguments.

lewislepton commented 9 years ago

thanks for that man. its just a bit confusing to say the least ;) because its making the image, but then having to pass it on etc for the main.lua because its create rederers etc that is tripping me up the most and this passing of data.

if you could, id love if there were a cheap and simple example to take apart and learn from

thanks again

lewislepton commented 9 years ago

but thanks again for the help. just more examples that can show what it can do would be great. things to take apart and really show lua-SDL2 in action

lewislepton commented 9 years ago

i still want to use lua-SDL2, absolutely. just looking at other frameworks purely for speedy creation ;)