Why does CPointer<T>.pointed not work


#1

Hi there,
I’m playing around with SDL and am currently stuck.

package main

import kotlinx.cinterop.CPointer
import sdl.*

fun main(args: Array<String>) {
  SDL_Init(SDL_INIT_VIDEO)
  val window = SDL_CreateWindow("test", 10, 10, 800, 600, SDL_WINDOW_OPENGL)
  val surface: CPointer<SDL_Surface>? = SDL_GetWindowSurface(window)
  if (surface != null) {
    //                       the error is there  -v
    SDL_FillRect(surface, null, SDL_MapRGB(surface.pointed.format, 255, 255, 255))
  }
  SDL_Delay(5000)
  SDL_DestroyWindow(window)
  SDL_Quit()
}

the compiler always quits with “unresolved reference: pointed” and I don’t understand why.

In the docs there is

val <T : CPointed> CPointer<T>.pointed : T

https://kotlinlang.org/api/latest/jvm/stdlib/kotlinx.cinterop/-c-pointer/index.html#extension-properties
and SDL_Surface is a subtype of CPointed.
What am I missing?


#2

You probably need to import the extension property. Something like

import kotlinx.cinterop.*

or

import kotlinx.cinterop.pointed

should probably work (untested)


#3

Yeah that was it. I didn’t know that extensions had to be explicitly imported. The linter complains about wildcard imports so I avoid them… Well now it works, so thank you!


#4

There is a setting about that in intellij. You can set it to either use no wildcards, always use them or have more then x number of imports from the same package. You can also specify packages which are imported using wildcards only.